]> git.sesse.net Git - freerainbowtables/blob - Common/rt api/RTIReader.cpp
b72dbe2d214c4fdcee088957bbe6e32ef9ec3d21
[freerainbowtables] / Common / rt api / RTIReader.cpp
1 #include "RTIReader.h"
2
3 RTIReader::RTIReader(string Filename)
4 {
5         m_pIndex = NULL;
6         m_pFile = fopen(Filename.c_str(), "rb");
7         if(m_pFile == NULL) {
8                 printf("could not open file %s\n", Filename.c_str());
9                 return;         
10         }
11         string sIndex = Filename.append(".index").c_str();
12         FILE *pFileIndex = fopen(sIndex.c_str(), "rb");
13         if(pFileIndex == NULL) {
14                 printf("could not open index file %s\n", sIndex.c_str());
15                 return;
16         }
17         m_chainPosition = 0;
18
19         // Load the index file
20         unsigned int nIndexFileLen = GetFileLen(pFileIndex);
21         unsigned int nFileLen = GetFileLen(m_pFile);
22         unsigned int nTotalChainCount = nFileLen / 8;
23         if (nFileLen % 8 != 0)
24                 printf("file length mismatch (%u bytes)\n", nFileLen);
25         else
26         {
27                 // File length check
28                 if (nIndexFileLen % 11 != 0)
29                         printf("index file length mismatch (%u bytes)\n", nIndexFileLen);
30                 else
31                 {
32                         m_pIndex = new IndexChain[nIndexFileLen / 11];
33                         memset(m_pIndex, 0x00, sizeof(IndexChain) * (nIndexFileLen / 11));
34                         fseek(pFileIndex, 0, SEEK_SET);
35                         int nRead = 0;
36                         int nRows;
37                         for(nRows = 0; (nRows * 11) < nIndexFileLen; nRows++)
38                         {
39                                 if(fread(&m_pIndex[nRows].nPrefix, 5, 1, pFileIndex) != 1) break;                                                       
40                                 if(fread(&m_pIndex[nRows].nFirstChain, 4, 1, pFileIndex) != 1) break;                                                   
41                                 if(fread(&m_pIndex[nRows].nChainCount, 2, 1, pFileIndex) != 1) break;                                                   
42                                 // Index checking part
43                                 if(nRows != 0 && m_pIndex[nRows].nFirstChain < m_pIndex[nRows-1].nFirstChain)
44                                 {
45                                         printf("Corrupted index detected (FirstChain is lower than previous)\n");
46                                         exit(-1);
47                                 }
48                                 else if(nRows != 0 && m_pIndex[nRows].nFirstChain != m_pIndex[nRows-1].nFirstChain + m_pIndex[nRows-1].nChainCount)
49                                 {
50                                         printf("Corrupted index detected (LastChain + nChainCount != FirstChain)\n");
51                                         exit(-1);
52                                 }
53                                 
54                         }
55                         m_nIndexSize = nRows;
56                         if(m_pIndex[m_nIndexSize - 1].nFirstChain + m_pIndex[m_nIndexSize - 1].nChainCount + 1 <= nTotalChainCount) // +1 is needed.
57                         {
58                                 printf("Corrupted index detected: Not covering the entire file\n");
59                                 exit(-1);
60                         }
61                         if(m_pIndex[m_nIndexSize - 1].nFirstChain + m_pIndex[m_nIndexSize - 1].nChainCount > nTotalChainCount) // +1 is not needed here
62                         {
63                                 printf("Corrupted index detected: The index is covering more than the file\n");
64                                 exit(-1);
65                         }
66
67         /*                                      if(nIndexSize != pIndex[i].nFirstChain + pIndex[i].nChainCount)
68                         {
69                                 printf("Index is not covering the entire tables\n");
70                         }*/
71                         fclose(pFileIndex);             
72         //                                      printf("debug: Index loaded successfully (%u entries)\n", nIndexSize);
73                 }               
74         }
75
76
77 }
78
79 int RTIReader::ReadChains(unsigned int &numChains, RainbowChainCP *pData)
80 {       
81         // We HAVE to reset the data to 0x00's or we will get in trouble
82         memset(pData, 0x00, sizeof(RainbowChainCP) * numChains);
83         unsigned int readChains = 0;
84         unsigned int chainsleft = GetChainsLeft();
85         for(int i = 0; i < m_nIndexSize; i++)
86         {
87                 if(m_chainPosition + readChains > m_pIndex[i].nFirstChain + m_pIndex[i].nChainCount) // We found the matching index
88                         continue;
89                 while(m_chainPosition + readChains < m_pIndex[i].nFirstChain + m_pIndex[i].nChainCount)
90                 {
91                         pData[readChains].nIndexE = m_pIndex[i].nPrefix << 16;
92                         int endpoint = 0; // We have to set it to 0
93                         fread(&pData[readChains].nIndexS, 6, 1, m_pFile);
94                         fread(&endpoint, 2, 1, m_pFile);
95                         pData[readChains].nIndexE += endpoint;
96                         readChains++;
97                         if(readChains == numChains || readChains == chainsleft) break;
98                 }
99                 if(readChains == numChains) break;              
100         }
101         if(readChains != numChains) numChains = readChains; // Update how many chains we read
102         m_chainPosition += readChains;
103         return 0;
104 }
105
106 unsigned int RTIReader::GetChainsLeft()
107 {
108         int len = GetFileLen(m_pFile) / 8 - m_chainPosition;
109         return len;
110 }
111
112 RTIReader::~RTIReader(void)
113 {
114         if(m_pIndex != NULL)
115                 delete m_pIndex;
116         if(m_pFile != NULL)
117                 fclose(m_pFile);
118
119 }