]> git.sesse.net Git - freerainbowtables/blob - Common/rt api/RTIReader.cpp
(C)
[freerainbowtables] / Common / rt api / RTIReader.cpp
1 /*
2  * freerainbowtables is a project for generating, distributing, and using
3  * perfect rainbow tables
4  *
5  * Copyright 2010, 2011 Martin Westergaard Jørgensen <martinwj2005@gmail.com>
6  * Copyright 2010, 2011 James Nobis <frt@quelrod.net>
7  *
8  * This file is part of freerainbowtables.
9  *
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.
14  *
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.
19  *
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/>.
22  */
23
24 #include "RTIReader.h"
25
26 RTIReader::RTIReader(string Filename)
27 {
28         m_pIndex = NULL;
29         m_pFile = fopen(Filename.c_str(), "rb");
30         if(m_pFile == NULL) {
31                 printf("could not open file %s\n", Filename.c_str());
32                 return;         
33         }
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());
38                 return;
39         }
40         m_chainPosition = 0;
41
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);
48         else
49         {
50                 // File length check
51                 if (nIndexFileLen % 11 != 0)
52                         printf("index file length mismatch (%lu bytes)\n", nIndexFileLen);
53                 else
54                 {
55                         if(m_pIndex != NULL) {
56                                 delete m_pIndex;
57                                 m_pIndex = NULL;
58                         }
59 #ifdef _MEMORYDEBUG
60                         printf("Allocating %u MB memory for RTIReader::m_pIndex", nIndexFileLen / 11 / (1024 * 1024));
61 #endif
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));
65                                 exit(-2);
66                         }
67 #ifdef _MEMORYDEBUG
68                         printf(" - success!\n");
69 #endif                  
70                         memset(m_pIndex, 0x00, sizeof(IndexChain) * (nIndexFileLen / 11));
71                         fseek(pFileIndex, 0, SEEK_SET);
72                         //int nRead = 0;
73                         uint32 nRows;
74                         for(nRows = 0; (nRows * 11) < (uint32)nIndexFileLen; nRows++)
75                         {
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)
81                                 {
82                                         printf("Corrupted index detected (FirstChain is lower than previous)\n");
83                                         exit(-1);
84                                 }
85                                 else if(nRows != 0 && m_pIndex[nRows].nFirstChain != m_pIndex[nRows-1].nFirstChain + m_pIndex[nRows-1].nChainCount)
86                                 {
87                                         printf("Corrupted index detected (LastChain + nChainCount != FirstChain)\n");
88                                         exit(-1);
89                                 }
90                                 
91                         }
92                         m_nIndexSize = nRows;
93                         if(m_pIndex[m_nIndexSize - 1].nFirstChain + m_pIndex[m_nIndexSize - 1].nChainCount + 1 <= nTotalChainCount) // +1 is needed.
94                         {
95                                 printf("Corrupted index detected: Not covering the entire file\n");
96                                 exit(-1);
97                         }
98                         if(m_pIndex[m_nIndexSize - 1].nFirstChain + m_pIndex[m_nIndexSize - 1].nChainCount > nTotalChainCount) // +1 is not needed here
99                         {
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);
101                                 exit(-1);
102                         }
103
104         /*                                      if(nIndexSize != pIndex[i].nFirstChain + pIndex[i].nChainCount)
105                         {
106                                 printf("Index is not covering the entire tables\n");
107                         }*/
108                         fclose(pFileIndex);             
109         //                                      printf("debug: Index loaded successfully (%u entries)\n", nIndexSize);
110                 }               
111         }
112
113
114 }
115
116 int RTIReader::ReadChains(uint32 &numChains, RainbowChain *pData)
117 {       
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++)
123         {
124                 if(m_chainPosition + readChains > m_pIndex[i].nFirstChain + m_pIndex[i].nChainCount) // We found the matching index
125                         continue;
126                 while(m_chainPosition + readChains < m_pIndex[i].nFirstChain + m_pIndex[i].nChainCount)
127                 {
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;
133                         readChains++;
134                         if(readChains == numChains || readChains == chainsleft) break;
135                 }
136                 if(readChains == numChains) break;              
137         }
138         if(readChains != numChains) { 
139                 numChains = readChains; // Update how many chains we read
140         }
141         m_chainPosition += readChains;
142         printf("Chain position is now %u\n", m_chainPosition);
143         return 0;
144 }
145
146 uint32 RTIReader::GetChainsLeft()
147 {       
148         return (GetFileLen(m_pFile) / 8) - m_chainPosition;
149 }
150
151 RTIReader::~RTIReader(void)
152 {
153         if(m_pIndex != NULL)
154                 delete m_pIndex;
155         if(m_pFile != NULL)
156                 fclose(m_pFile);
157
158 }