]> git.sesse.net Git - freerainbowtables/blob - Common/rt api/RTI2Reader.cpp
443fd1c7029d81fcc566a047cedabe25717559af
[freerainbowtables] / Common / rt api / RTI2Reader.cpp
1 #include "RTI2Reader.h"
2
3 #include <math.h>
4 RTI2Header *RTI2Reader::m_pHeader = NULL;
5 RTI2Reader::RTI2Reader(string Filename)
6 {
7         //m_pIndexPos = NULL, m_pChainPos = NULL;;
8         m_pIndex = NULL;
9         m_pFile = fopen(Filename.c_str(), "rb");
10         if(m_pFile == NULL)
11         {
12                 printf("Unable to open file %s", Filename.c_str());
13                 exit(1);
14         }
15         FILE *pFileIndex = fopen(Filename.append(".index").c_str(), "rb");
16         if(pFileIndex == NULL)
17         {
18                 printf("Unable to open file %s", Filename.append(".index").c_str());
19                 exit(1);
20         }
21         m_chainPosition = 0;
22
23         unsigned int len = GetFileLen(pFileIndex);
24         fseek(pFileIndex, 0, SEEK_SET);
25
26         m_pIndex = new unsigned char[len];
27         if(fread(m_pIndex, 1, len, pFileIndex) != len)
28         {
29                 printf("Error while reading index file");
30                 exit(1);
31         }
32         fclose(pFileIndex);
33         m_pHeader = new RTI2Header();   
34         memcpy(m_pHeader, m_pIndex, sizeof(RTI2Header));
35         m_pHeader->m_cppos = (unsigned int*)(m_pIndex + 8);
36         m_pHeader->prefixstart = *(uint64*)(m_pIndex + 8 + (m_pHeader->rti_cplength * 4));
37         m_chainsizebytes = ceil((float)(m_pHeader->rti_startptlength + m_pHeader->rti_endptlength + m_pHeader->rti_cplength) / 8); // Get the size of each chain in bytes
38         m_indexrowsizebytes = ceil((float)m_pHeader->rti_index_numchainslength / 8);
39         // Check the filesize
40         fseek(m_pFile, 0, SEEK_END);
41         len = ftell(m_pFile);
42         fseek(m_pFile, 0, SEEK_SET);
43         if(len % m_chainsizebytes > 0)
44         {
45                 printf("Invalid filesize %u\n", len);
46                 return;
47         }
48         
49
50 }
51
52 RTI2Reader::~RTI2Reader(void)
53 {
54         if(m_pIndex != NULL) delete m_pIndex;
55         if(m_pFile != NULL) fclose(m_pFile);
56
57 }
58
59 unsigned int RTI2Reader::GetChainsLeft()
60 {
61         int len = GetFileLen(m_pFile);
62         return len / m_chainsizebytes - m_chainPosition;
63 }
64
65 int RTI2Reader::ReadChains(unsigned int &numChains, RainbowChainCP *pData)
66 {
67         if(strncmp(m_pHeader->header, "RTI2", 4) != 0)
68         {
69                 numChains = 0;
70                 return -1;
71         }
72         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
73         unsigned int i = 0;
74         unsigned int indexRow = 0; // Current offset into the index
75         unsigned int curRowPosition = 0;
76         
77         while(true) // Fast forward to current position
78         {
79                 // ALERT: Possible problem here if m_indexrowsizebytes > 1 as pNumChains is a unsigned char.
80                 unsigned int NumChainsInRow = (unsigned int)*(pNumChains + indexRow * m_indexrowsizebytes);
81                 if(m_indexrowsizebytes > 1)     { printf("Have to find a solution to this problem"); exit(2);}
82                 if(i + NumChainsInRow > m_chainPosition)
83                 {
84                         curRowPosition = m_chainPosition - i;
85                         break; // The current position is somewhere within this prefix
86                 }
87                 indexRow++;             
88                 i += NumChainsInRow;
89         }
90         
91         uint64 chainrow = 0; // Buffer to store a single read chain
92         unsigned int chainsProcessed = 0; // Number of chains processed
93
94         // ALERT: same problem with unsigned char here.
95         unsigned int NumChainsInRow = *(pNumChains + indexRow);
96         while(chainsProcessed < numChains && fread(&chainrow, 1, m_chainsizebytes, m_pFile) == m_chainsizebytes)
97         {
98                 if(curRowPosition >= NumChainsInRow)
99                 { // Skip to next index row position
100                         indexRow++;
101                         curRowPosition = 0;
102                         NumChainsInRow = *(pNumChains + indexRow);
103                 }
104                 while(NumChainsInRow == 0) // We skip forward until we hit a index with > 0 chains
105                 {
106                         indexRow++;
107                         NumChainsInRow = *(pNumChains + indexRow);
108                         curRowPosition = 0;
109                 }
110                 // Load the starting point from the data
111                 pData[chainsProcessed].nIndexS = chainrow << 64 - m_pHeader->rti_startptlength;
112                 pData[chainsProcessed].nIndexS = pData[chainsProcessed].nIndexS >> 64 - m_pHeader->rti_startptlength;
113
114                 // Load the ending point prefix 
115                 pData[chainsProcessed].nIndexE = m_pHeader->prefixstart + indexRow << m_pHeader->rti_endptlength;
116                 // Append the ending point suffix
117                 pData[chainsProcessed].nIndexE |= (chainrow & (0xFFFFFFFFFFFFFFFF >> m_pHeader->rti_cplength)) >> m_pHeader->rti_startptlength;
118                 pData[chainsProcessed].nCheckPoint = (chainrow >> m_pHeader->rti_startptlength + m_pHeader->rti_endptlength);
119                 curRowPosition++;
120                 chainsProcessed++;
121         }
122         numChains = chainsProcessed;
123         m_chainPosition += numChains;
124         return 0;
125 }