36 PRAGMA48(GCC diagnostic ignored
"-Wshadow")
37 #include <SDL_endian.h>
42 #ifndef SDL_BIG_ENDIAN
43 #error missing SDL_endian.h
50 #define readVal(val, sz, msg) \
51 cnt = fread(static_cast<void*>(val), 1, sz, arcFile); \
54 reportAlways("Error reading " msg " in file %s", \
55 archiveName.c_str()) \
62 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
63 #define swapVal16(val) val = __builtin_bswap16(val);
64 #define swapVal32(val) val = __builtin_bswap32(val);
66 #define swapVal16(val)
67 #define swapVal32(val)
82 const std::string archiveName = entry->
root;
85 FILE *
restrict const arcFile = fopen(archiveName.c_str(),
87 if (arcFile ==
nullptr)
93 uint8_t *
const buf =
new uint8_t[65535 + 10];
99 logger->
log(
"Read archive: %s", archiveName.c_str());
103 while (feof(arcFile) == 0)
108 readVal(buf, 4,
"zip file header")
111 if (buf[0] == 0x50 &&
119 fseek(arcFile, 4, SEEK_CUR);
121 readVal(&method, 2,
"compression method")
125 fseek(arcFile, 8, SEEK_CUR);
128 "zip compressed size")
132 "zip uncompressed size")
135 readVal(&val16, 2,
"file name length")
138 const uint32_t fileNameLen =
CAST_U32(val16);
139 if (fileNameLen > 1000)
148 readVal(&val16, 2,
"extra field length")
151 const uint32_t extraFieldLen =
CAST_U32(val16);
152 readVal(buf, fileNameLen,
"file name")
154 buf[fileNameLen] = 0;
156 reinterpret_cast<char*
>(buf));
159 fseek(arcFile, extraFieldLen + header->
compressSize, SEEK_CUR);
163 headers.push_back(header);
185 else if (buf[0] == 0x50 &&
195 else if (buf[0] == 0x50 &&
207 reportAlways(
"Error in header signature (0x%02x%02x%02x%02x)"
237 return std::string();
239 return "unknown zlib error";
245 if (header ==
nullptr)
247 reportAlways(
"ZipReader::readCompressedFile: header is null")
250 FILE *
restrict const arcFile = fopen(
251 header->zipEntry->root.c_str(),
253 if (arcFile ==
nullptr)
256 header->zipEntry->root.c_str())
260 fseek(arcFile, header->dataOffset, SEEK_SET);
261 const uint32_t compressSize = header->compressSize;
262 uint8_t *
const buf =
new uint8_t[compressSize];
263 if (fread(
static_cast<void*
>(buf), 1, compressSize, arcFile) !=
266 reportAlways(
"Read zip compressed file error from archive: %s",
267 header->zipEntry->root.c_str())
278 if (header ==
nullptr)
286 if (header->compressed ==
false)
288 const size_t outSize = header->uncompressSize;
289 uint8_t *
restrict const out =
new uint8_t[outSize];
297 strm.zalloc =
nullptr;
298 strm.zfree =
nullptr;
299 strm.opaque =
nullptr;
301 strm.avail_in = header->compressSize;
307 int ret = inflateInit2(&strm, -MAX_WBITS);
317 ret = inflate(&strm, Z_FINISH);
#define reportAlways(...)
void log(const char *const log_text,...)
#define PRAGMACLANG6GCC(str)
bool dirs(InputEvent &event)
uint8_t * readFile(ZipLocalHeader *header)
uint8_t * readCompressedFile(ZipLocalHeader *header)
bool readArchiveInfo(ZipEntry *entry)
void reportZlibError(std::string &text, int err)
std::string getZlibError(int err)
void prepareFsPath(std::string &path)
bool findLast(const std::string &str1, const std::string &str2)
std::vector< std::string > mDirs
std::vector< ZipLocalHeader * > mHeaders
#define readVal(val, sz, msg)