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