X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=Common%2Frt%20api%2FRTIReader.cpp;h=374b062b5696ea6fa54144fff99cfd8eb7b87838;hb=86bbf0fd5ba4e07d3279b4179fd8fc808198eaae;hp=d96edc365c83569775c51e509f18b91dae400e8e;hpb=bcfd4a8e2d07b3a0fce0bc8e471d8562b142b7e0;p=freerainbowtables diff --git a/Common/rt api/RTIReader.cpp b/Common/rt api/RTIReader.cpp index d96edc3..374b062 100644 --- a/Common/rt api/RTIReader.cpp +++ b/Common/rt api/RTIReader.cpp @@ -4,28 +4,51 @@ RTIReader::RTIReader(string Filename) { m_pIndex = NULL; m_pFile = fopen(Filename.c_str(), "rb"); - FILE *pFileIndex = fopen(Filename.append(".index").c_str(), "rb"); + if(m_pFile == NULL) { + printf("could not open file %s\n", Filename.c_str()); + return; + } + string sIndex = Filename.append(".index").c_str(); + FILE *pFileIndex = fopen(sIndex.c_str(), "rb"); + if(pFileIndex == NULL) { + printf("could not open index file %s\n", sIndex.c_str()); + return; + } m_chainPosition = 0; // Load the index file - unsigned int nIndexFileLen = GetFileLen(pFileIndex); - unsigned int nFileLen = GetFileLen(m_pFile); + long nIndexFileLen = GetFileLen(pFileIndex); + long nFileLen = GetFileLen(m_pFile); unsigned int nTotalChainCount = nFileLen / 8; if (nFileLen % 8 != 0) - printf("file length mismatch (%u bytes)\n", nFileLen); + printf("file length mismatch (%lu bytes)\n", nFileLen); else { // File length check if (nIndexFileLen % 11 != 0) - printf("index file length mismatch (%u bytes)\n", nIndexFileLen); + printf("index file length mismatch (%lu bytes)\n", nIndexFileLen); else { - m_pIndex = new IndexChain[nIndexFileLen / 11]; + if(m_pIndex != NULL) { + delete m_pIndex; + m_pIndex = NULL; + } +#ifdef _MEMORYDEBUG + printf("Allocating %u MB memory for RTIReader::m_pIndex", nIndexFileLen / 11 / (1024 * 1024)); +#endif + m_pIndex = new (nothrow) IndexChain[nIndexFileLen / 11]; + if(m_pIndex == NULL) { + printf("\nFailed allocating %ld MB memory.\n", nIndexFileLen / 11 / (1024 * 1024)); + exit(-2); + } +#ifdef _MEMORYDEBUG + printf(" - success!\n"); +#endif memset(m_pIndex, 0x00, sizeof(IndexChain) * (nIndexFileLen / 11)); fseek(pFileIndex, 0, SEEK_SET); - int nRead = 0; - int nRows; - for(nRows = 0; (nRows * 11) < nIndexFileLen; nRows++) + //int nRead = 0; + uint32 nRows; + for(nRows = 0; (nRows * 11) < (uint32)nIndexFileLen; nRows++) { if(fread(&m_pIndex[nRows].nPrefix, 5, 1, pFileIndex) != 1) break; if(fread(&m_pIndex[nRows].nFirstChain, 4, 1, pFileIndex) != 1) break; @@ -51,7 +74,7 @@ RTIReader::RTIReader(string Filename) } if(m_pIndex[m_nIndexSize - 1].nFirstChain + m_pIndex[m_nIndexSize - 1].nChainCount > nTotalChainCount) // +1 is not needed here { - printf("Corrupted index detected: The index is covering more than the file\n"); + 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); exit(-1); } @@ -67,13 +90,13 @@ RTIReader::RTIReader(string Filename) } -int RTIReader::ReadChains(unsigned int &numChains, RainbowChainCP *pData) +int RTIReader::ReadChains(unsigned int &numChains, RainbowChain *pData) { // We HAVE to reset the data to 0x00's or we will get in trouble - memset(pData, 0x00, sizeof(RainbowChainCP) * numChains); + memset(pData, 0x00, sizeof(RainbowChain) * numChains); unsigned int readChains = 0; unsigned int chainsleft = GetChainsLeft(); - for(int i = 0; i < m_nIndexSize; i++) + for(UINT4 i = 0; i < m_nIndexSize; i++) { if(m_chainPosition + readChains > m_pIndex[i].nFirstChain + m_pIndex[i].nChainCount) // We found the matching index continue; @@ -89,15 +112,17 @@ int RTIReader::ReadChains(unsigned int &numChains, RainbowChainCP *pData) } if(readChains == numChains) break; } - if(readChains != numChains) numChains = readChains; // Update how many chains we read + if(readChains != numChains) { + numChains = readChains; // Update how many chains we read + } m_chainPosition += readChains; + printf("Chain position is now %u\n", m_chainPosition); return 0; } -unsigned int RTIReader::GetChainsLeft() -{ - int len = GetFileLen(m_pFile) / 8 - m_chainPosition; - return len; +UINT4 RTIReader::GetChainsLeft() +{ + return (GetFileLen(m_pFile) / 8) - m_chainPosition; } RTIReader::~RTIReader(void)