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