]> git.sesse.net Git - freerainbowtables/commitdiff
Modify ChainWalkSet::FindInFile() to keep the precalc index in memory instead of...
authorSteinar H. Gunderson <Steinar H. Gunderson sesse@debian.org>
Mon, 21 Feb 2011 22:38:35 +0000 (23:38 +0100)
committerSteinar H. Gunderson <Steinar H. Gunderson sesse@debian.org>
Mon, 21 Feb 2011 22:38:35 +0000 (23:38 +0100)
Client Applications/rcracki_mt/ChainWalkSet.cpp
Client Applications/rcracki_mt/ChainWalkSet.h
Client Applications/rcracki_mt/Public.h

index 61a2ec06cd1146b13b156e0f7e7bace44d4a0c66..9aafcda6b12c3bd689db33df80d951e13362abe8 100644 (file)
@@ -57,7 +57,7 @@ void CChainWalkSet::DiscardAll()
        m_lChainWalk.clear();\r
 }\r
 \r
-string CChainWalkSet::CheckOrRotatePreCalcFile()\r
+int CChainWalkSet::CheckOrRotatePreCalcFile()\r
 {\r
        char sPreCalcFileName[255];\r
 \r
@@ -81,19 +81,20 @@ string CChainWalkSet::CheckOrRotatePreCalcFile()
                                // We don't as only newly generated chainwalksets will be stored to this new file, so we don't have to look there\r
                                if (debug) printf("Debug: Using for precalc: %s\n", sReturnPreCalcPath.c_str());\r
                                fclose(file);\r
-                               return sReturnPreCalcPath;\r
+                               return preCalcPart;\r
                        }\r
                        fclose(file);\r
                }\r
        }\r
 \r
-       return string("");\r
+       return -1;\r
 }\r
 \r
 void CChainWalkSet::updateUsedPrecalcFiles()\r
 {\r
        // we might also use this function to search a wildcard path of precalc files\r
        vPrecalcFiles.clear();\r
+       mPrecalcIndex.clear();\r
        char sPreCalcFileName[255];\r
 \r
        int i;\r
@@ -103,13 +104,35 @@ void CChainWalkSet::updateUsedPrecalcFiles()
                sprintf(sPreCalcFileName, "%s.%d", sPrecalcPathName.c_str(), i);\r
                string sTryPreCalcPath(sPreCalcFileName);\r
                FILE* file = fopen(sTryPreCalcPath.c_str(), "rb");\r
-               if(file!=NULL) {\r
-                       vPrecalcFiles.push_back(sTryPreCalcPath);\r
-                       fclose(file);\r
-               }\r
-               else {\r
+               if(file==NULL) {\r
                        break;\r
                }\r
+\r
+               vPrecalcFiles.push_back(sTryPreCalcPath);\r
+               fclose(file);\r
+\r
+               // Load the index file into our cache.\r
+               string sCurrentPrecalcIndexPathName = sTryPreCalcPath + ".index";\r
+               vector<string> precalcLines;\r
+               if (!ReadLinesFromFile(sCurrentPrecalcIndexPathName.c_str(), precalcLines)) {\r
+                       continue;\r
+               }\r
+\r
+               long unsigned int offset = 0;\r
+               for (int j = 0; j < (int)precalcLines.size(); j++)\r
+               {\r
+                       // Parse\r
+                       vector<string> vPart;\r
+                       if (!SeparateString(precalcLines[j], "___:", vPart))\r
+                       {\r
+                               // corrupt file\r
+                               printf("Corrupted precalculation file!\n");\r
+                               break;\r
+                       }\r
+\r
+                       mPrecalcIndex[precalcLines[j]] = make_pair(i, offset);\r
+                       offset += ((atoi(vPart[3].c_str())-1) * sizeof(uint64));\r
+               }\r
        }\r
 }\r
 \r
@@ -137,92 +160,62 @@ void CChainWalkSet::removePrecalcFiles()
                        if (debug) printf("Debug: Failed removing precalc index file: %s\n", sCurrentPrecalcIndexPathName.c_str());\r
 \r
        }\r
+\r
+       mPrecalcIndex.clear();\r
 }\r
 \r
 bool CChainWalkSet::FindInFile(uint64* pIndexE, unsigned char* pHash, int nHashLen)\r
 {\r
-       int gotPrecalcOnLine = -1;\r
        char precalculationLine[255];\r
-       sprintf(precalculationLine, "%s_%s#%d-%d_%d_%d:%s\n", m_sHashRoutineName.c_str(), m_sPlainCharsetName.c_str(), m_nPlainLenMin, m_nPlainLenMax, m_nRainbowTableIndex, m_nRainbowChainLen, HexToStr(pHash, nHashLen).c_str() );\r
-       string precalcString(precalculationLine);\r
+       sprintf(precalculationLine, "%s_%s#%d-%d_%d_%d:%s", m_sHashRoutineName.c_str(), m_sPlainCharsetName.c_str(), m_nPlainLenMin, m_nPlainLenMax, m_nRainbowTableIndex, m_nRainbowChainLen, HexToStr(pHash, nHashLen).c_str() );\r
 \r
-       string sCurrentPrecalcPathName = "";\r
-       string sCurrentPrecalcIndexPathName = "";\r
-       long unsigned int offset;\r
-\r
-       int i;\r
-       for (i = 0; i < (int)vPrecalcFiles.size() && gotPrecalcOnLine == -1; i++)\r
-       {\r
-               sCurrentPrecalcPathName = vPrecalcFiles[i];\r
-               sCurrentPrecalcIndexPathName = sCurrentPrecalcPathName + ".index";\r
-\r
-               offset = 0;\r
-\r
-               vector<string> precalcLines;\r
-               if (ReadLinesFromFile(sCurrentPrecalcIndexPathName.c_str(), precalcLines))\r
-               {\r
-                       int j;\r
-                       for (j = 0; j < (int)precalcLines.size(); j++)\r
-                       {\r
-                               if (precalcString.compare(0, precalcString.size()-1, precalcLines[j]) == 0)\r
-                               {\r
-                                       gotPrecalcOnLine = j;\r
-                                       break;\r
-                               }\r
-\r
-                               // Parse\r
-                               vector<string> vPart;\r
-                               if (SeparateString(precalcLines[j], "___:", vPart))\r
-                               {\r
-                                       // add to offset\r
-                                       offset += ((atoi(vPart[3].c_str())-1) * sizeof(uint64));\r
-                               }\r
-                               else {\r
-                                       // corrupt file\r
-                                       printf("Corrupted precalculation file!\n");\r
-                                       gotPrecalcOnLine = -1;\r
-                                       break;\r
-                               }\r
-                       }\r
-               }\r
+       map<string, pair<int, unsigned long int> >::const_iterator precalc_it =\r
+           mPrecalcIndex.find(precalculationLine);\r
+       if (precalc_it == mPrecalcIndex.end()) {\r
+               return false;\r
        }\r
 \r
-       if (gotPrecalcOnLine > -1)\r
-       {\r
-               if (debug) printf("Debug: Reading pre calculations from file, line %d offset %lu\n", gotPrecalcOnLine, offset);\r
-               \r
-               FILE* fp = fopen(sCurrentPrecalcPathName.c_str(), "rb");\r
+       int fileNumber = precalc_it->second.first;\r
+       unsigned long int offset = precalc_it->second.second;\r
+       if (debug) printf("Debug: Reading pre calculations from file, file %d offset %lu\n", fileNumber, offset);\r
 \r
-               if (fp!=NULL) {\r
-                       fseek(fp, offset, SEEK_SET);\r
+       string sCurrentPrecalcPathName = vPrecalcFiles[fileNumber];\r
+       FILE* fp = fopen(sCurrentPrecalcPathName.c_str(), "rb");\r
 \r
-                       // We should do some verification here, for example by recalculating the middle chain, to catch corrupted files\r
-                       if(fread(pIndexE, sizeof(uint64), (unsigned long)m_nRainbowChainLen-1, fp) != (unsigned long)m_nRainbowChainLen-1)\r
-                               printf("File read error.");\r
-                       fclose(fp);\r
-               }\r
-               else\r
-                       printf("Cannot open precalculation file %s.\n", sCurrentPrecalcPathName.c_str());\r
+       if (fp == NULL) {\r
+               printf("Cannot open precalculation file %s.\n", sCurrentPrecalcPathName.c_str());\r
+               return false;\r
+       }\r
 \r
-               //printf("\npIndexE[0]: %s\n", uint64tostr(pIndexE[0]).c_str());\r
-               //printf("\npIndexE[nRainbowChainLen-2]: %s\n", uint64tostr(pIndexE[m_nRainbowChainLen-2]).c_str());\r
+       fseek(fp, offset, SEEK_SET);\r
 \r
-               return true;\r
+       // We should do some verification here, for example by recalculating the middle chain, to catch corrupted files\r
+       if(fread(pIndexE, sizeof(uint64), (unsigned long)m_nRainbowChainLen-1, fp) != (unsigned long)m_nRainbowChainLen-1) {\r
+               printf("File read error.");\r
+               return false;\r
        }\r
 \r
-       return false;\r
+       fclose(fp);\r
+       return true;\r
 }\r
 \r
 void CChainWalkSet::StoreToFile(uint64* pIndexE, unsigned char* pHash, int nHashLen)\r
 {\r
        if (debug) printf("\nDebug: Storing precalc\n");\r
        \r
-       string sCurrentPrecalcPathName = CheckOrRotatePreCalcFile();\r
-       string sCurrentPrecalcIndexPathName = sCurrentPrecalcPathName + ".index";\r
+       int iCurrentPrecalcPart = CheckOrRotatePreCalcFile();\r
+       if (iCurrentPrecalcPart == -1) {\r
+               return;\r
+       }\r
+       char sCurrentPrecalcPathName[256];\r
+       sprintf(sCurrentPrecalcPathName, "%s.%d", sPrecalcPathName.c_str(), preCalcPart);\r
+       string sCurrentPrecalcIndexPathName = string(sCurrentPrecalcPathName) + ".index";\r
 \r
-       FILE* fp = fopen(sCurrentPrecalcPathName.c_str(), "ab");\r
+       FILE* fp = fopen(sCurrentPrecalcPathName, "ab");\r
        if(fp!=NULL)\r
        {\r
+               unsigned long int offset = GetFileLen(fp);\r
+\r
                if(fwrite(pIndexE, sizeof(uint64), (unsigned long)m_nRainbowChainLen-1, fp) != (unsigned long)m_nRainbowChainLen-1)\r
                        printf("File write error.");\r
                else\r
@@ -234,12 +227,13 @@ void CChainWalkSet::StoreToFile(uint64* pIndexE, unsigned char* pHash, int nHash
                                sprintf(precalculationLine, "%s_%s#%d-%d_%d_%d:%s\n", m_sHashRoutineName.c_str(), m_sPlainCharsetName.c_str(), m_nPlainLenMin, m_nPlainLenMax, m_nRainbowTableIndex, m_nRainbowChainLen, HexToStr(pHash, nHashLen).c_str() );\r
                                fputs (precalculationLine, file);\r
                                fclose (file);\r
+                               mPrecalcIndex[TrimString(precalculationLine)] = make_pair(iCurrentPrecalcPart, offset);\r
                        }\r
                }\r
                fclose(fp);\r
                }\r
        else\r
-               printf("Cannot open precalculation file %s\n", sCurrentPrecalcPathName.c_str());\r
+               printf("Cannot open precalculation file %s\n", sCurrentPrecalcPathName);\r
 }\r
 \r
 uint64* CChainWalkSet::RequestWalk(unsigned char* pHash, int nHashLen,\r
index a89c38ca9cde05b11008d247ea8a136920ee2b79..a29fc016a054e64f6d299d0603914c58ce0d60bd 100644 (file)
@@ -53,11 +53,12 @@ private:
        string sPrecalcPathName;\r
        int    preCalcPart;\r
        vector<string> vPrecalcFiles;\r
+       map<string, pair<int, unsigned long int> > mPrecalcIndex;  // key -> (file number, file offset)\r
 \r
 private:\r
        void DiscardAll();\r
        bool FindInFile(uint64* pIndexE, unsigned char* pHash, int nHashLen);\r
-       string CheckOrRotatePreCalcFile();\r
+       int CheckOrRotatePreCalcFile();\r
        void updateUsedPrecalcFiles();\r
 \r
 public:\r
index a2773815934259cf2c7809e20354115cf2a38a6e..df741b21b4f4322f00a0140d0dea85bb6b357ff1 100644 (file)
@@ -33,6 +33,7 @@
 #include <vector>\r
 #include <list>\r
 #include <map>\r
+#include <utility>\r
 \r
 #include "global.h"\r
 \r