]> git.sesse.net Git - freerainbowtables/blob - Client Applications/rcracki_mt/RTI2Reader.cpp
UINT4 -> uint32
[freerainbowtables] / Client Applications / rcracki_mt / RTI2Reader.cpp
1 /*
2  * rcracki_mt is a multithreaded implementation and fork of the original 
3  * RainbowCrack
4  *
5  * Copyright 2010 Martin Westergaard Jørgensen <martinwj2005@gmail.com>
6  * Copyright 2010 Daniël Niggebrugge <niggebrugge@fox-it.com>
7  * Copyright 2010 James Nobis <frt@quelrod.net>
8  *
9  * This file is part of rcracki_mt.
10  *
11  * rcracki_mt is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * rcracki_mt is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with rcracki_mt.  If not, see <http://www.gnu.org/licenses/>.
23  */
24
25 #include "RTI2Reader.h"
26
27 #include <math.h>
28
29 RTI2Header *RTI2Reader::m_pHeader = NULL;
30 RTI2Reader::RTI2Reader(string Filename)
31 {
32         //m_pIndexPos = NULL, m_pChainPos = NULL;;
33         m_pIndex = NULL;
34         m_pFile = fopen(Filename.c_str(), "rb");
35         if(m_pFile == NULL)
36         {
37                 printf("Unable to open file %s", Filename.c_str());
38                 exit(1);
39         }
40         FILE *pFileIndex = fopen(Filename.append(".index").c_str(), "rb");
41         if(pFileIndex == NULL)
42         {
43                 printf("Unable to open file %s", Filename.append(".index").c_str());
44                 exit(1);
45         }
46         m_chainPosition = 0;
47
48         long len = GetFileLen(pFileIndex);
49         fseek(pFileIndex, 0, SEEK_SET);
50
51         m_pIndex = new unsigned char[len];
52         if(fread(m_pIndex, 1, len, pFileIndex) != (unsigned long)len)
53         {
54                 printf("Error while reading index file");
55                 exit(1);
56         }
57         fclose(pFileIndex);
58         m_pHeader = new RTI2Header();   
59         memcpy(m_pHeader, m_pIndex, sizeof(RTI2Header));
60         m_pHeader->m_cppos = (unsigned int*)(m_pIndex + 8);
61         m_pHeader->prefixstart = *(uint64*)(m_pIndex + 8 + (m_pHeader->rti_cplength * 4));
62         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
63         m_indexrowsizebytes = (uint32)ceil((float)m_pHeader->rti_index_numchainslength / 8);
64         // Check the filesize
65         fseek(m_pFile, 0, SEEK_END);
66         len = ftell(m_pFile);
67         fseek(m_pFile, 0, SEEK_SET);
68         if(len % m_chainsizebytes > 0)
69         {
70                 printf("Invalid filesize %ld\n", len);
71                 return;
72         }
73         
74
75 }
76
77 RTI2Reader::~RTI2Reader(void)
78 {
79         if(m_pIndex != NULL) delete m_pIndex;
80         if(m_pFile != NULL) fclose(m_pFile);
81
82 }
83
84 unsigned int RTI2Reader::GetChainsLeft()
85 {
86         long len = GetFileLen(m_pFile);
87         return len / m_chainsizebytes - m_chainPosition;
88 }
89
90 int RTI2Reader::ReadChains(unsigned int &numChains, RainbowChainO *pData)
91 {
92         if(strncmp(m_pHeader->header, "RTI2", 4) != 0)
93         {
94                 numChains = 0;
95                 return -1;
96         }
97         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 i = 0;
99         unsigned int indexRow = 0; // Current offset into the index
100         unsigned int curRowPosition = 0;
101         
102         while(true) // Fast forward to current position
103         {
104                 // ALERT: Possible problem here if m_indexrowsizebytes > 1 as pNumChains is a unsigned char.
105                 unsigned int NumChainsInRow = (unsigned int)*(pNumChains + indexRow * m_indexrowsizebytes);
106                 if(m_indexrowsizebytes > 1)
107                 {
108                         //XXX Have to find a solution to this problem
109                         printf( "FATAL: m_indexrowsizebytes > 1: %d\n", m_indexrowsizebytes ); 
110                         exit(2);
111                 }
112                 if(i + NumChainsInRow > m_chainPosition)
113                 {
114                         curRowPosition = m_chainPosition - i;
115                         break; // The current position is somewhere within this prefix
116                 }
117                 indexRow++;             
118                 i += NumChainsInRow;
119         }
120         
121         uint64 chainrow = 0; // Buffer to store a single read chain
122         unsigned int chainsProcessed = 0; // Number of chains processed
123
124         // XXX: same problem with unsigned char here.
125         unsigned int NumChainsInRow = *(pNumChains + indexRow);
126         while(chainsProcessed < numChains && fread(&chainrow, 1, m_chainsizebytes, m_pFile) == m_chainsizebytes)
127         {
128                 if(curRowPosition >= NumChainsInRow)
129                 { // Skip to next index row position
130                         indexRow++;
131                         curRowPosition = 0;
132                         NumChainsInRow = *(pNumChains + indexRow);
133                 }
134                 while(NumChainsInRow == 0) // We skip forward until we hit a index with > 0 chains
135                 {
136                         indexRow++;
137                         NumChainsInRow = *(pNumChains + indexRow);
138                         curRowPosition = 0;
139                 }
140                 // Load the starting point from the data
141                 pData[chainsProcessed].nIndexS = chainrow << ( 64 - m_pHeader->rti_startptlength );
142                 pData[chainsProcessed].nIndexS = pData[chainsProcessed].nIndexS >> ( 64 - m_pHeader->rti_startptlength );
143
144                 // Load the ending point prefix 
145                 pData[chainsProcessed].nIndexE = ( m_pHeader->prefixstart + indexRow ) << m_pHeader->rti_endptlength;
146                 // Append the ending point suffix
147 #if defined(_WIN32) && !defined(__GNUC__)
148                 pData[chainsProcessed].nIndexE |= (chainrow & (0xFFFFFFFFFFFFFFFFI64 >> m_pHeader->rti_cplength)) >> m_pHeader->rti_startptlength;
149 #else
150                 pData[chainsProcessed].nIndexE |= (chainrow & (0xFFFFFFFFFFFFFFFFllu >> m_pHeader->rti_cplength)) >> m_pHeader->rti_startptlength;
151 #endif
152                 //pData[chainsProcessed].nCheckPoint = (chainrow >> m_pHeader->rti_startptlength + m_pHeader->rti_endptlength);
153                 curRowPosition++;
154                 chainsProcessed++;
155         }
156         numChains = chainsProcessed;
157         m_chainPosition += numChains;
158         return 0;
159 }