3 RTIReader::RTIReader(string Filename)
6 m_pFile = fopen(Filename.c_str(), "rb");
8 printf("could not open file %s\n", Filename.c_str());
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());
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);
28 if (nIndexFileLen % 11 != 0)
29 printf("index file length mismatch (%u bytes)\n", nIndexFileLen);
32 if(m_pIndex != NULL) {
37 printf("Allocating %u MB memory for RTIReader::m_pIndex", nIndexFileLen / 11 / (1024 * 1024));
39 m_pIndex = new (nothrow) IndexChain[nIndexFileLen / 11];
40 if(m_pIndex == NULL) {
41 printf("\nFailed allocating %i MB memory.\n", nIndexFileLen / 11 / (1024 * 1024));
45 printf(" - success!\n");
47 memset(m_pIndex, 0x00, sizeof(IndexChain) * (nIndexFileLen / 11));
48 fseek(pFileIndex, 0, SEEK_SET);
51 for(nRows = 0; (nRows * 11) < nIndexFileLen; nRows++)
53 if(fread(&m_pIndex[nRows].nPrefix, 5, 1, pFileIndex) != 1) break;
54 if(fread(&m_pIndex[nRows].nFirstChain, 4, 1, pFileIndex) != 1) break;
55 if(fread(&m_pIndex[nRows].nChainCount, 2, 1, pFileIndex) != 1) break;
56 // Index checking part
57 if(nRows != 0 && m_pIndex[nRows].nFirstChain < m_pIndex[nRows-1].nFirstChain)
59 printf("Corrupted index detected (FirstChain is lower than previous)\n");
62 else if(nRows != 0 && m_pIndex[nRows].nFirstChain != m_pIndex[nRows-1].nFirstChain + m_pIndex[nRows-1].nChainCount)
64 printf("Corrupted index detected (LastChain + nChainCount != FirstChain)\n");
70 if(m_pIndex[m_nIndexSize - 1].nFirstChain + m_pIndex[m_nIndexSize - 1].nChainCount + 1 <= nTotalChainCount) // +1 is needed.
72 printf("Corrupted index detected: Not covering the entire file\n");
75 if(m_pIndex[m_nIndexSize - 1].nFirstChain + m_pIndex[m_nIndexSize - 1].nChainCount > nTotalChainCount) // +1 is not needed here
77 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);
81 /* if(nIndexSize != pIndex[i].nFirstChain + pIndex[i].nChainCount)
83 printf("Index is not covering the entire tables\n");
86 // printf("debug: Index loaded successfully (%u entries)\n", nIndexSize);
93 int RTIReader::ReadChains(unsigned int &numChains, RainbowChain *pData)
95 // We HAVE to reset the data to 0x00's or we will get in trouble
96 memset(pData, 0x00, sizeof(RainbowChain) * numChains);
97 unsigned int readChains = 0;
98 unsigned int chainsleft = GetChainsLeft();
99 for(UINT4 i = 0; i < m_nIndexSize; i++)
101 if(m_chainPosition + readChains > m_pIndex[i].nFirstChain + m_pIndex[i].nChainCount) // We found the matching index
103 while(m_chainPosition + readChains < m_pIndex[i].nFirstChain + m_pIndex[i].nChainCount)
105 pData[readChains].nIndexE = m_pIndex[i].nPrefix << 16;
106 int endpoint = 0; // We have to set it to 0
107 fread(&pData[readChains].nIndexS, 6, 1, m_pFile);
108 fread(&endpoint, 2, 1, m_pFile);
109 pData[readChains].nIndexE += endpoint;
111 if(readChains == numChains || readChains == chainsleft) break;
113 if(readChains == numChains) break;
115 if(readChains != numChains) numChains = readChains; // Update how many chains we read
116 m_chainPosition += readChains;
120 unsigned int RTIReader::GetChainsLeft()
122 int len = GetFileLen(m_pFile) / 8 - m_chainPosition;
126 RTIReader::~RTIReader(void)