From af2a5b93f2fe55c36186f5dc82a04eb3422f2000 Mon Sep 17 00:00:00 2001 From: orignal Date: Thu, 20 Nov 2014 14:29:22 -0500 Subject: [PATCH] SU3 file parser --- I2PEndian.h | 8 ++++++++ Reseed.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Reseed.h | 3 ++- 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/I2PEndian.h b/I2PEndian.h index 34153dd4..f7a2a95f 100644 --- a/I2PEndian.h +++ b/I2PEndian.h @@ -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__ diff --git a/Reseed.cpp b/Reseed.cpp index e68f313c..368d4455 100644 --- a/Reseed.cpp +++ b/Reseed.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #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); + } + } } diff --git a/Reseed.h b/Reseed.h index 3b07d840..2b5b8272 100644 --- a/Reseed.h +++ b/Reseed.h @@ -17,7 +17,8 @@ namespace data bool reseedNow(); }; + void ProcessSU3File (const char * filename); } } -#endif \ No newline at end of file +#endif