2 * freerainbowtables is a project for generating, distributing, and using
3 * perfect rainbow tables
5 * Copyright 2010, 2011 Martin Westergaard Jørgensen <martinwj2005@gmail.com>
6 * Copyright 2010, 2011 James Nobis <frt@quelrod.net>
8 * This file is part of freerainbowtables.
10 * freerainbowtables is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 2 of the License, or
13 * (at your option) any later version.
15 * freerainbowtables is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with freerainbowtables. If not, see <http://www.gnu.org/licenses/>.
24 #include "RTIReader.h"
26 RTIReader::RTIReader(string Filename)
29 m_pFile = fopen(Filename.c_str(), "rb");
31 printf("could not open file %s\n", Filename.c_str());
34 string sIndex = Filename.append(".index").c_str();
35 FILE *pFileIndex = fopen(sIndex.c_str(), "rb");
36 if(pFileIndex == NULL) {
37 printf("could not open index file %s\n", sIndex.c_str());
42 // Load the index file
43 long nIndexFileLen = GetFileLen(pFileIndex);
44 long nFileLen = GetFileLen(m_pFile);
45 unsigned int nTotalChainCount = nFileLen / 8;
46 if (nFileLen % 8 != 0)
47 printf("file length mismatch (%lu bytes)\n", nFileLen);
51 if (nIndexFileLen % 11 != 0)
52 printf("index file length mismatch (%lu bytes)\n", nIndexFileLen);
55 if(m_pIndex != NULL) {
60 printf("Allocating %u MB memory for RTIReader::m_pIndex", nIndexFileLen / 11 / (1024 * 1024));
62 m_pIndex = new (nothrow) IndexChain[nIndexFileLen / 11];
63 if(m_pIndex == NULL) {
64 printf("\nFailed allocating %ld MB memory.\n", nIndexFileLen / 11 / (1024 * 1024));
68 printf(" - success!\n");
70 memset(m_pIndex, 0x00, sizeof(IndexChain) * (nIndexFileLen / 11));
71 fseek(pFileIndex, 0, SEEK_SET);
74 for(nRows = 0; (nRows * 11) < (uint32)nIndexFileLen; nRows++)
76 if(fread(&m_pIndex[nRows].nPrefix, 5, 1, pFileIndex) != 1) break;
77 if(fread(&m_pIndex[nRows].nFirstChain, 4, 1, pFileIndex) != 1) break;
78 if(fread(&m_pIndex[nRows].nChainCount, 2, 1, pFileIndex) != 1) break;
79 // Index checking part
80 if(nRows != 0 && m_pIndex[nRows].nFirstChain < m_pIndex[nRows-1].nFirstChain)
82 printf("Corrupted index detected (FirstChain is lower than previous)\n");
85 else if(nRows != 0 && m_pIndex[nRows].nFirstChain != m_pIndex[nRows-1].nFirstChain + m_pIndex[nRows-1].nChainCount)
87 printf("Corrupted index detected (LastChain + nChainCount != FirstChain)\n");
93 if(m_pIndex[m_nIndexSize - 1].nFirstChain + m_pIndex[m_nIndexSize - 1].nChainCount + 1 <= nTotalChainCount) // +1 is needed.
95 printf("Corrupted index detected: Not covering the entire file\n");
98 if(m_pIndex[m_nIndexSize - 1].nFirstChain + m_pIndex[m_nIndexSize - 1].nChainCount > nTotalChainCount) // +1 is not needed here
100 printf("Corrupted index detected: The index is covering more than the file (%i chains of %i chains)\n", m_pIndex[m_nIndexSize - 1].nFirstChain + m_pIndex[m_nIndexSize - 1].nChainCount, nTotalChainCount);
104 /* if(nIndexSize != pIndex[i].nFirstChain + pIndex[i].nChainCount)
106 printf("Index is not covering the entire tables\n");
109 // printf("debug: Index loaded successfully (%u entries)\n", nIndexSize);
116 int RTIReader::ReadChains(uint32 &numChains, RainbowChain *pData)
118 // We HAVE to reset the data to 0x00's or we will get in trouble
119 memset(pData, 0x00, sizeof(RainbowChain) * numChains);
120 unsigned int readChains = 0;
121 unsigned int chainsleft = GetChainsLeft();
122 for(uint32 i = 0; i < m_nIndexSize; i++)
124 if(m_chainPosition + readChains > m_pIndex[i].nFirstChain + m_pIndex[i].nChainCount) // We found the matching index
126 while(m_chainPosition + readChains < m_pIndex[i].nFirstChain + m_pIndex[i].nChainCount)
128 pData[readChains].nIndexE = m_pIndex[i].nPrefix << 16;
129 int endpoint = 0; // We have to set it to 0
130 fread(&pData[readChains].nIndexS, 6, 1, m_pFile);
131 fread(&endpoint, 2, 1, m_pFile);
132 pData[readChains].nIndexE += endpoint;
134 if(readChains == numChains || readChains == chainsleft) break;
136 if(readChains == numChains) break;
138 if(readChains != numChains) {
139 numChains = readChains; // Update how many chains we read
141 m_chainPosition += readChains;
142 printf("Chain position is now %u\n", m_chainPosition);
146 uint32 RTIReader::GetChainsLeft()
148 return (GetFileLen(m_pFile) / 8) - m_chainPosition;
151 RTIReader::~RTIReader(void)