]> git.sesse.net Git - freerainbowtables/blobdiff - Client Applications/rcracki_mt/CrackEngine.cpp
Merge branch 'master' of git@gitorious.org:freerainbowtables-applications/freerainbow...
[freerainbowtables] / Client Applications / rcracki_mt / CrackEngine.cpp
index 00216101becbcd26586209c93fd8431a0bf85a3a..0343588f38015dcead7af0a10818659b97a01cc4 100644 (file)
@@ -3,12 +3,12 @@
  * RainbowCrack\r
  *\r
  * Copyright (C) Zhu Shuanglei <shuanglei@hotmail.com>\r
- * Copyright Martin Westergaard Jørgensen <martinwj2005@gmail.com>\r
+ * Copyright 2009, 2010 Martin Westergaard Jørgensen <martinwj2005@gmail.com>\r
  * Copyright 2009, 2010 Daniël Niggebrugge <niggebrugge@fox-it.com>\r
  * Copyright 2009, 2010 James Nobis <frt@quelrod.net>\r
  * Copyright 2010 uroskn\r
  *\r
- * This file is part of racrcki_mt.\r
+ * This file is part of rcracki_mt.\r
  *\r
  * rcracki_mt is free software: you can redistribute it and/or modify\r
  * it under the terms of the GNU General Public License as published by\r
  * along with rcracki_mt.  If not, see <http://www.gnu.org/licenses/>.\r
  */\r
 \r
-#ifdef _WIN32\r
+#if defined(_WIN32) && !defined(__GNUC__)\r
        #pragma warning(disable : 4786 4267 4018)\r
 #endif\r
 \r
 #include "CrackEngine.h"\r
 #include "RTI2Reader.h"\r
 \r
+#ifndef _WIN32\r
+       #include <sys/resource.h>\r
+#endif\r
+\r
 CCrackEngine::CCrackEngine()\r
 {\r
        ResetStatistics();\r
@@ -299,19 +303,32 @@ void CCrackEngine::SearchTableChunkOld(RainbowChainO* pChain, int nRainbowChainL
        vector<rcrackiThread*> threadPool;\r
        vector<pthread_t> pThreads;\r
 \r
+       #ifndef _WIN32\r
+               /*\r
+                * On linux you cannot set the priority of a thread in the non real time\r
+                * scheduling groups.  You can set the priority of the process.  In\r
+                * windows BELOW_NORMAL represents a 1/8th drop in priority and this would\r
+                * be 20 * 1/8 on linux or about 2.5\r
+                */\r
+               setpriority( PRIO_PROCESS, 0, 2 );\r
+       #endif\r
+\r
        pthread_attr_t attr;\r
        pthread_attr_init(&attr);\r
        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);\r
        #ifdef _WIN32\r
        sched_param param;\r
+       /*\r
+        * windows scheduling is 0 to 32 (low to high) with 8 as normal and 7 as\r
+        * BELOW_NORMAL\r
+        */\r
        param.sched_priority = THREAD_PRIORITY_BELOW_NORMAL;\r
        pthread_attr_setschedparam (&attr, &param);\r
        #endif\r
-       // XXX else set it to 5 or something (for linux)?\r
 \r
        bool pausing = false;\r
 \r
-       UINT4 nHashIndex;\r
+       uint32 nHashIndex;\r
        for (nHashIndex = 0; nHashIndex < vHash.size(); nHashIndex++)\r
        {\r
                #ifdef _WIN32\r
@@ -425,7 +442,7 @@ void CCrackEngine::SearchTableChunkOld(RainbowChainO* pChain, int nRainbowChainL
                        threadPool.clear();\r
                        pThreads.clear();\r
                        \r
-                       UINT4 thread_ID;\r
+                       uint32 thread_ID;\r
                        for (thread_ID = 0; thread_ID < (unsigned long)maxThreads; thread_ID++)\r
                        {\r
                                rcrackiThread* r_Thread = new rcrackiThread(TargetHash, thread_ID, nRainbowChainLen, maxThreads, pStartPosIndexE);\r
@@ -494,7 +511,7 @@ void CCrackEngine::SearchTableChunkOld(RainbowChainO* pChain, int nRainbowChainL
                        threadPool.push_back(r_Thread);\r
                }\r
 \r
-               UINT4 thread_ID = 0;\r
+               uint32 thread_ID = 0;\r
                int nPos;\r
                for (nPos = nRainbowChainLen - 2; nPos >= 0; nPos--)\r
                {\r
@@ -615,6 +632,16 @@ void CCrackEngine::SearchTableChunk(RainbowChain* pChain, int nRainbowChainLen,
 \r
        vector<rcrackiThread*> threadPool;\r
        vector<pthread_t> pThreads;\r
+       \r
+       #ifndef _WIN32\r
+               /*\r
+                * On linux you cannot set the priority of a thread in the non real time\r
+                * scheduling groups.  You can set the priority of the process.  In\r
+                * windows BELOW_NORMAL represents a 1/8th drop in priority and this would\r
+                * be 20 * 1/8 on linux or about 2.5\r
+                */\r
+               setpriority( PRIO_PROCESS, 0, 2 );\r
+       #endif\r
 \r
        pthread_attr_t attr;\r
        pthread_attr_init(&attr);\r
@@ -628,7 +655,7 @@ void CCrackEngine::SearchTableChunk(RainbowChain* pChain, int nRainbowChainLen,
 \r
        bool pausing = false;\r
 \r
-       UINT4 nHashIndex;\r
+       uint32 nHashIndex;\r
        for (nHashIndex = 0; nHashIndex < vHash.size(); nHashIndex++)\r
        {\r
                #ifdef _WIN32\r
@@ -745,7 +772,7 @@ void CCrackEngine::SearchTableChunk(RainbowChain* pChain, int nRainbowChainLen,
                        threadPool.clear();\r
                        pThreads.clear();\r
                        \r
-                       UINT4 thread_ID;\r
+                       uint32 thread_ID;\r
                        for (thread_ID = 0; thread_ID < (unsigned long)maxThreads; thread_ID++)\r
                        {\r
                                rcrackiThread* r_Thread = new rcrackiThread(TargetHash, thread_ID, nRainbowChainLen, maxThreads, pStartPosIndexE);\r
@@ -819,7 +846,7 @@ void CCrackEngine::SearchTableChunk(RainbowChain* pChain, int nRainbowChainLen,
                        threadPool.push_back(r_Thread);\r
                }\r
 \r
-               UINT4 thread_ID = 0;\r
+               uint32 thread_ID = 0;\r
                int nPos;\r
                for (nPos = nRainbowChainLen - 2; nPos >= 0; nPos--)\r
                {\r
@@ -929,7 +956,7 @@ void CCrackEngine::SearchRainbowTable(string sPathName, CHashSet& hs)
                vector<string> sessionFinishedPathNames;\r
                if (ReadLinesFromFile(sProgressPathName.c_str(), sessionFinishedPathNames))\r
                {\r
-                       UINT4 i;\r
+                       uint32 i;\r
                        for (i = 0; i < sessionFinishedPathNames.size(); i++)\r
                        {\r
                                if (sessionFinishedPathNames[i] == sPathName)\r
@@ -943,12 +970,12 @@ void CCrackEngine::SearchRainbowTable(string sPathName, CHashSet& hs)
 \r
        // FileName\r
 #ifdef _WIN32\r
-       int nIndex = sPathName.find_last_of('\\');\r
+       string::size_type nIndex = sPathName.find_last_of('\\');\r
 #else\r
-       int nIndex = (int) sPathName.find_last_of('/');\r
+       string::size_type nIndex = sPathName.find_last_of('/');\r
 #endif\r
        string sFileName;\r
-       if (nIndex != -1)\r
+       if (nIndex != string::npos)\r
                sFileName = sPathName.substr(nIndex + 1);\r
        else\r
                sFileName = sPathName;\r
@@ -975,9 +1002,9 @@ void CCrackEngine::SearchRainbowTable(string sPathName, CHashSet& hs)
                // File length check\r
                bool doOldFormat = CChainWalkContext::isOldFormat();\r
                bool doRti2Format = CChainWalkContext::isRti2Format();\r
-               UINT4 sizeOfChain;\r
+               uint32 sizeOfChain;\r
                bool fVerified = false;\r
-               UINT4 nFileLen = GetFileLen(file);\r
+               long nFileLen = GetFileLen(file);\r
 \r
                if (doOldFormat)\r
                        sizeOfChain = 16;\r
@@ -985,7 +1012,7 @@ void CCrackEngine::SearchRainbowTable(string sPathName, CHashSet& hs)
                        sizeOfChain = 8;\r
 \r
                //if (nFileLen % 8 != 0 || nRainbowChainCount * 8 != nFileLen)\r
-               if ( (nFileLen % sizeOfChain != 0 || nRainbowChainCount * sizeOfChain != nFileLen) && doRti2Format == false )\r
+               if ( ( (unsigned long)nFileLen % sizeOfChain != 0 || nRainbowChainCount * sizeOfChain != (unsigned long)nFileLen) && doRti2Format == false )\r
                        printf("file length mismatch\n");\r
                else\r
                {\r
@@ -1017,8 +1044,18 @@ void CCrackEngine::SearchRainbowTable(string sPathName, CHashSet& hs)
                                }\r
 \r
                                static CMemoryPool mp(bytesForChainWalkSet, debug, maxMem);\r
-                               RainbowChainO* pChain = (RainbowChainO*)mp.Allocate(nFileLen, nAllocatedSize);\r
-                               if (debug) printf("Allocated %llu bytes, filelen %lu\n", nAllocatedSize, (unsigned long)nFileLen);\r
+                               RainbowChainO* pChain = NULL;\r
+                               if(doRti2Format) {\r
+                                       pChain = (RainbowChainO*)mp.Allocate(pReader->GetChainsLeft() * 16, nAllocatedSize);\r
+                               } else {\r
+                                       pChain = (RainbowChainO*)mp.Allocate(nFileLen, nAllocatedSize);\r
+                               }\r
+                               #if defined(_WIN32) && !defined(__GNUC__)\r
+                                       if (debug) printf("Allocated %I64 bytes, filelen %ld\n", nAllocatedSize, nFileLen);\r
+                               #else\r
+                                       if (debug) printf("Allocated %llu bytes, filelen %ld\n", nAllocatedSize, nFileLen);\r
+                               #endif\r
+\r
                                if (pChain != NULL)\r
                                {\r
                                        nAllocatedSize = nAllocatedSize / sizeOfChain * sizeOfChain;            // Round to sizeOfChain boundary\r
@@ -1027,7 +1064,7 @@ void CCrackEngine::SearchRainbowTable(string sPathName, CHashSet& hs)
                                        //bool fVerified = false;\r
                                        while (true)    // Chunk read loop\r
                                        {\r
-                                               if ((unsigned long)ftell(file) == nFileLen)\r
+                                               if (ftell(file) == nFileLen)\r
                                                        break;\r
 \r
                                                // Load table chunk\r
@@ -1037,11 +1074,11 @@ void CCrackEngine::SearchRainbowTable(string sPathName, CHashSet& hs)
                                                if ( doRti2Format )\r
                                                {\r
                                                        nDataRead = nAllocatedSize / 16;\r
+                                                       if(pReader->GetChainsLeft() <= 0) // No more data\r
+                                                               break; \r
                                                        pReader->ReadChains(nDataRead, pChain);\r
-                                                       nDataRead *= 8; // Convert from chains read to bytes\r
 \r
-                                                       if ( nDataRead == 0 ) // No more data\r
-                                                               break;\r
+                                                       nDataRead *= 8; // Convert from chains read to bytes\r
                                                }\r
                                                else\r
                                                {\r
@@ -1056,6 +1093,10 @@ void CCrackEngine::SearchRainbowTable(string sPathName, CHashSet& hs)
 \r
                                                int nRainbowChainCountRead = nDataRead / 16;\r
 \r
+                                               if(doRti2Format) {\r
+                                                       nRainbowChainCountRead = nDataRead / 8;\r
+                                               }\r
+\r
                                                // Verify table chunk\r
                                                if (!fVerified)\r
                                                {\r
@@ -1106,6 +1147,16 @@ void CCrackEngine::SearchRainbowTable(string sPathName, CHashSet& hs)
                                                // Already finished?\r
                                                if (!hs.AnyHashLeftWithLen(CChainWalkContext::GetHashLen()))\r
                                                        break;\r
+\r
+/*\r
+       // XXX eliminated by PB - check correctness\r
+                                               // finished the current table\r
+                                               if( doRti2Format && nDataToRead > (nDataRead / 8) )\r
+                                               {\r
+                                                       delete pReader;\r
+                                                       break;\r
+                                               }\r
+*/\r
                                        }\r
                                }\r
                                else\r
@@ -1125,18 +1176,22 @@ void CCrackEngine::SearchRainbowTable(string sPathName, CHashSet& hs)
                                if(fIndex != NULL)\r
                                {\r
                                        // File length check\r
-                                       unsigned int nFileLenIndex = GetFileLen(fIndex);\r
+                                       long nFileLenIndex = GetFileLen(fIndex);\r
                                        //unsigned int nRows = nFileLenIndex / 11;\r
                                        //unsigned int nSize = nRows * sizeof(IndexChain);\r
                                        //printf("Debug: 8\n");\r
                                        if (nFileLenIndex % 11 != 0)\r
-                                               printf("index file length mismatch (%u bytes)\n", nFileLenIndex);\r
+                                               printf("index file length mismatch (%ld bytes)\n", nFileLenIndex);\r
                                        else\r
                                        {\r
                                                //printf("index nSize: %d\n", nSize);\r
                                                //pIndex = (IndexChain*)new unsigned char[nSize];\r
                                                IndexChain *pIndex = (IndexChain*)mpIndex.Allocate(nFileLenIndex, nAllocatedSizeIndex);\r
-                                               if (debug) printf("Debug: Allocated %llu bytes for index with filelen %u\n", nAllocatedSizeIndex, nFileLenIndex);\r
+                                               #ifdef _WIN32\r
+                                                       if (debug) printf("Debug: Allocated %I64u bytes for index with filelen %ld\n", nAllocatedSizeIndex, nFileLenIndex);\r
+                                               #else\r
+                                                       if (debug) printf("Debug: Allocated %llu bytes for index with filelen %ld\n", nAllocatedSizeIndex, nFileLenIndex);\r
+                                               #endif\r
                                \r
                                                static CMemoryPool mp(bytesForChainWalkSet + nAllocatedSizeIndex, debug, maxMem);\r
                                                \r
@@ -1146,10 +1201,14 @@ void CCrackEngine::SearchRainbowTable(string sPathName, CHashSet& hs)
                                                \r
                                                        fseek(fIndex, 0, SEEK_SET);\r
 \r
-                                                       while ( (unsigned long)ftell(fIndex) != nFileLenIndex ) // Index chunk read loop\r
+                                                       while ( ftell(fIndex) != nFileLenIndex )        // Index chunk read loop\r
                                                        {\r
                                                                // Load index chunk\r
+#ifdef _WIN32\r
+                                                               if (debug) printf("Debug: Setting index to 0x00 in memory, %I64u bytes\n", nAllocatedSizeIndex);\r
+#else\r
                                                                if (debug) printf("Debug: Setting index to 0x00 in memory, %llu bytes\n", nAllocatedSizeIndex);\r
+#endif\r
                                                                memset(pIndex, 0x00, nAllocatedSizeIndex);\r
                                                                printf("reading index... ");\r
                                                                gettimeofday( &tv, NULL );\r
@@ -1172,7 +1231,11 @@ void CCrackEngine::SearchRainbowTable(string sPathName, CHashSet& hs)
 \r
                                                                //RainbowChain* pChain = (RainbowChain*)mp.Allocate(nFileLen, nAllocatedSize);\r
                                                                RainbowChain* pChain = (RainbowChain*)mp.Allocate(nCoveredRainbowTableChains * sizeOfChain, nAllocatedSize);\r
-                                                               if (debug) printf("Debug: Allocated %llu bytes for %u chains, filelen %lu\n", nAllocatedSize, nCoveredRainbowTableChains, (unsigned long)nFileLen);\r
+                                                               #ifdef _WIN32\r
+                                                                       if (debug) printf("Debug: Allocated %I64u bytes for %u chains, filelen %lu\n", nAllocatedSize, nCoveredRainbowTableChains, (unsigned long)nFileLen);\r
+                                                               #else\r
+                                                                       if (debug) printf("Debug: Allocated %llu bytes for %u chains, filelen %lu\n", nAllocatedSize, nCoveredRainbowTableChains, (unsigned long)nFileLen);\r
+                                                               #endif\r
 \r
                                                                if (pChain != NULL && nAllocatedSize > 0)\r
                                                                {\r
@@ -1180,8 +1243,8 @@ void CCrackEngine::SearchRainbowTable(string sPathName, CHashSet& hs)
 \r
                                                                        //fseek(file, 0, SEEK_SET);\r
                                                                        //bool fVerified = false;\r
-                                                                       UINT4 nProcessedChains = 0;\r
-                                                                       while ( (unsigned long)ftell(file) != nFileLen \r
+                                                                       uint32 nProcessedChains = 0;\r
+                                                                       while ( ftell(file) != nFileLen \r
                                                                                && nProcessedChains < nCoveredRainbowTableChains )      // Chunk read loop\r
                                                                        {\r
                                                                                // Load table chunk\r
@@ -1311,7 +1374,7 @@ void CCrackEngine::Run(vector<string> vPathName, CHashSet& hs, int i_maxThreads,
        ResetStatistics();\r
 \r
        // Sort vPathName (CChainWalkSet need it)\r
-       UINT4 i, j;\r
+       uint32 i, j;\r
        for (i = 0; i < vPathName.size() - 1; i++)\r
                for (j = 0; j < vPathName.size() - i - 1; j++)\r
                {\r