2 * freerainbowtables is a project for generating, distributing, and using
3 * perfect rainbow tables
5 * Copyright 2010 Martin Westergaard Jørgensen <martinwj2005@gmail.com>
6 * Copyright 2010 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 "RTI2Reader.h"
28 RTI2Header *RTI2Reader::m_pHeader = NULL;
29 RTI2Reader::RTI2Reader(string Filename)
31 //m_pIndexPos = NULL, m_pChainPos = NULL;;
33 m_pFile = fopen(Filename.c_str(), "rb");
36 printf("Unable to open file %s", Filename.c_str());
39 FILE *pFileIndex = fopen(Filename.append(".index").c_str(), "rb");
40 if(pFileIndex == NULL)
42 printf("Unable to open file %s", Filename.append(".index").c_str());
47 long len = GetFileLen(pFileIndex);
48 fseek(pFileIndex, 0, SEEK_SET);
50 m_pIndex = new unsigned char[len];
51 if(fread(m_pIndex, 1, len, pFileIndex) != (unsigned long)len)
53 printf("Error while reading index file");
57 m_pHeader = new RTI2Header();
58 memcpy(m_pHeader, m_pIndex, sizeof(RTI2Header));
59 m_pHeader->m_cppos = (unsigned int*)(m_pIndex + 8);
60 m_pHeader->prefixstart = *(uint64*)(m_pIndex + 8 + (m_pHeader->rti_cplength * 4));
61 m_chainsizebytes = (uint32)ceil((float)(m_pHeader->rti_startptlength + m_pHeader->rti_endptlength + m_pHeader->rti_cplength) / 8); // Get the size of each chain in bytes
62 m_indexrowsizebytes = (uint32)ceil((float)m_pHeader->rti_index_numchainslength / 8);
64 fseek(m_pFile, 0, SEEK_END);
66 fseek(m_pFile, 0, SEEK_SET);
67 if(len % m_chainsizebytes > 0)
69 printf("Invalid filesize %ld\n", len);
76 RTI2Reader::~RTI2Reader(void)
78 if(m_pIndex != NULL) delete m_pIndex;
79 if(m_pFile != NULL) fclose(m_pFile);
83 unsigned int RTI2Reader::GetChainsLeft()
85 long len = GetFileLen(m_pFile);
86 return len / m_chainsizebytes - m_chainPosition;
89 int RTI2Reader::ReadChains(unsigned int &numChains, RainbowChainCP *pData)
91 if(strncmp(m_pHeader->header, "RTI2", 4) != 0)
96 unsigned char *pNumChains = m_pIndex + (m_pHeader->rti_cplength * 4) + 16; // Pointer into the index containing info about how many numbers are in the first chain prefix
98 unsigned int indexRow = 0; // Current offset into the index
99 unsigned int curRowPosition = 0;
101 while(true) // Fast forward to current position
103 // ALERT: Possible problem here if m_indexrowsizebytes > 1 as pNumChains is a unsigned char.
104 unsigned int NumChainsInRow = (unsigned int)*(pNumChains + indexRow * m_indexrowsizebytes);
105 if(m_indexrowsizebytes > 1)
107 //XXX Have to find a solution to this problem
108 printf( "FATAL: m_indexrowsizebytes > 1: %d\n", m_indexrowsizebytes );
111 if(i + NumChainsInRow > m_chainPosition)
113 curRowPosition = m_chainPosition - i;
114 break; // The current position is somewhere within this prefix
120 uint64 chainrow = 0; // Buffer to store a single read chain
121 unsigned int chainsProcessed = 0; // Number of chains processed
123 // XXX: same problem with unsigned char here.
124 unsigned int NumChainsInRow = *(pNumChains + indexRow);
125 while(chainsProcessed < numChains && fread(&chainrow, 1, m_chainsizebytes, m_pFile) == m_chainsizebytes)
127 if(curRowPosition >= NumChainsInRow)
128 { // Skip to next index row position
131 NumChainsInRow = *(pNumChains + indexRow);
133 while(NumChainsInRow == 0) // We skip forward until we hit a index with > 0 chains
136 NumChainsInRow = *(pNumChains + indexRow);
139 // Load the starting point from the data
140 pData[chainsProcessed].nIndexS = chainrow << ( 64 - m_pHeader->rti_startptlength );
141 pData[chainsProcessed].nIndexS = pData[chainsProcessed].nIndexS >> ( 64 - m_pHeader->rti_startptlength );
143 // Load the ending point prefix
144 pData[chainsProcessed].nIndexE = ( m_pHeader->prefixstart + indexRow ) << m_pHeader->rti_endptlength;
145 // Append the ending point suffix
146 #if defined(_WIN32) && !defined(__GNUC__)
147 pData[chainsProcessed].nIndexE |= (chainrow & (0xFFFFFFFFFFFFFFFFI64 >> m_pHeader->rti_cplength)) >> m_pHeader->rti_startptlength;
149 pData[chainsProcessed].nIndexE |= (chainrow & (0xFFFFFFFFFFFFFFFFllu >> m_pHeader->rti_cplength)) >> m_pHeader->rti_startptlength;
151 //pData[chainsProcessed].nCheckPoint = (chainrow >> m_pHeader->rti_startptlength + m_pHeader->rti_endptlength);
155 numChains = chainsProcessed;
156 m_chainPosition += numChains;