m_lChainWalk.clear();\r
}\r
\r
-string CChainWalkSet::CheckOrRotatePreCalcFile()\r
+int CChainWalkSet::CheckOrRotatePreCalcFile()\r
{\r
char sPreCalcFileName[255];\r
\r
// 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
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
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 (SeperateString(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
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