SU3 file parser

This commit is contained in:
orignal 2014-11-20 14:29:22 -05:00
parent f9ec60265a
commit af2a5b93f2
3 changed files with 64 additions and 1 deletions

View File

@ -34,6 +34,14 @@ uint16_t be16toh(uint16_t big16);
uint32_t be32toh(uint32_t big32);
uint64_t be64toh(uint64_t big64);
// assume LittleEndine
#define htole16
#define htole32
#define htole64
#define le16toh
#define le32toh
#define le64toh
#endif
#endif // I2PENDIAN_H__

View File

@ -2,6 +2,7 @@
#include <fstream>
#include <boost/regex.hpp>
#include <boost/filesystem.hpp>
#include <cryptopp/gzip.h>
#include "Reseed.h"
#include "Log.h"
#include "util.h"
@ -119,6 +120,59 @@ namespace data
return false;
}
void ProcessSU3File (const char * filename)
{
static uint32_t headerSignature = htole32 (0x04044B50);
std::ifstream s(filename, std::ifstream::binary);
if (s.is_open ())
{
while (!s.eof ())
{
uint32_t signature;
s.read ((char *)&signature, 4);
if (signature == headerSignature)
{
// next local file
s.seekg (14, std::ios::cur); // skip field we don't care about
uint32_t compressedSize, uncompressedSize;
s.read ((char *)&compressedSize, 4);
compressedSize = le32toh (compressedSize);
s.read ((char *)&uncompressedSize, 4);
uncompressedSize = le32toh (uncompressedSize);
uint16_t fileNameLength, extraFieldLength;
s.read ((char *)&fileNameLength, 2);
fileNameLength = le32toh (fileNameLength);
s.read ((char *)&extraFieldLength, 2);
extraFieldLength = le32toh (extraFieldLength);
char localFileName[255];
s.read (localFileName, fileNameLength);
localFileName[fileNameLength] = 0;
s.seekg (extraFieldLength, std::ios::cur);
uint8_t * compressed = new uint8_t[compressedSize];
s.read ((char *)compressed, compressedSize);
CryptoPP::Gunzip decompressor;
decompressor.Put (compressed, compressedSize);
delete[] compressed;
if (decompressor.MaxRetrievable () <= uncompressedSize)
{
uint8_t * uncompressed = new uint8_t[uncompressedSize];
decompressor.Get (uncompressed, decompressor.MaxRetrievable ());
// TODO: save file
delete[] uncompressed;
}
else
LogPrint (eLogError, "Actual uncompressed size ", decompressor.MaxRetrievable (), " exceed ", uncompressedSize, " from header");
}
else
break; // no more files
}
}
else
LogPrint (eLogError, "Can't open file ", filename);
}
}
}

View File

@ -17,7 +17,8 @@ namespace data
bool reseedNow();
};
void ProcessSU3File (const char * filename);
}
}
#endif
#endif