summaryrefslogtreecommitdiff
path: root/src/fs/virtfs/zip.cpp
blob: a94c804e883cb4446d7cac52a3fd7f000a36e0a3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
 *  The ManaPlus Client
 *  Copyright (C) 2017  The ManaPlus Developers
 *
 *  This file is part of The ManaPlus Client.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef USE_PHYSFS

#include "fs/virtfs/zip.h"

#include "fs/paths.h"

#include "fs/virtfs/virtzipentry.h"
#include "fs/virtfs/ziplocalheader.h"

#include "utils/checkutils.h"
#include "utils/stringutils.h"

#include <iostream>
#include <unistd.h>
#include <zlib.h>

#include "debug.h"

//#define DEBUG_ZIP

extern const char *dirSeparator;

#define readVal(val, sz, msg) \
    cnt = fread(static_cast<void*>(val), 1, sz, arcFile); \
    if (cnt != sz) \
    { \
        reportAlways("Error reading " msg " in file %s", \
            archiveName.c_str()); \
        delete header; \
        delete [] buf; \
        fclose(arcFile); \
        return false; \
    } \

namespace Zip
{
    bool readArchiveInfo(VirtZipEntry *const entry)
    {
        if (entry == nullptr)
        {
            reportAlways("Entry is null.");
            return false;
        }
        const std::string archiveName = entry->root;
        std::vector<ZipLocalHeader*> &restrict headers = entry->mHeaders;
        std::vector<std::string> &restrict dirs = entry->mDirs;
        FILE *restrict const arcFile = fopen(archiveName.c_str(),
            "rb");
        if (arcFile == nullptr)
        {
            reportAlways("Can't open zip file %s",
                archiveName.c_str());
            return false;
        }
        size_t cnt = 0U;
        uint8_t *const buf = new uint8_t[65535 + 10];
        uint16_t val16 = 0U;
        uint16_t method = 0U;
        ZipLocalHeader *header = nullptr;

#ifdef DEBUG_ZIP
        logger->log("Read archive: %s", archiveName.c_str());
#endif  // DEBUG_ZIP

        // format source https://en.wikipedia.org/wiki/Zip_%28file_format%29
        while (feof(arcFile) == 0)
        {
            // file header pointer on 0
            // read file header signature
            readVal(buf, 4, "zip file header");  // + 4
            // pointer on 4

            if (buf[0] == 0x50 &&
                buf[1] == 0x4B &&
                buf[2] == 0x03 &&
                buf[3] == 0x04)
            {   // local file header
                header = new ZipLocalHeader;
                header->zipEntry = entry;
                // skip useless fields
                fseek(arcFile, 4, SEEK_CUR);  // + 4
                // file header pointer on 8
                // +++ need add endian specific decoding for method
                readVal(&method, 2, "compression method")  // + 2
                header->compressed = (method != 0);
                // file header pointer on 10
                fseek(arcFile, 8, SEEK_CUR);  // + 8
                // file header pointer on 18
                readVal(&header->compressSize, 4,
                    "zip compressed size")  // + 4
                // file header pointer on 22
                // +++ need add endian specific decoding for val32
                readVal(&header->uncompressSize, 4,
                    "zip uncompressed size")  // + 4
                // file header pointer on 26
                // +++ need add endian specific decoding for val32
                readVal(&val16, 2, "file name length")  // + 2
                // file header pointer on 28
                const uint32_t fileNameLen = CAST_U32(val16);
                if (fileNameLen > 1000)
                {
                    reportAlways("Error too long file name in file %s",
                        archiveName.c_str());
                    delete header;
                    delete [] buf;
                    fclose(arcFile);
                    return false;
                }
                readVal(&val16, 2, "extra field length")  // + 2
                // file header pointer on 30
                const uint32_t extraFieldLen = CAST_U32(val16);
                readVal(buf, fileNameLen, "file name");
                // file header pointer on 30 + fileNameLen
                buf[fileNameLen] = 0;
                header->fileName = std::string(
                    reinterpret_cast<char*>(buf));
                prepareFsPath(header->fileName);
                header->dataOffset = ftell(arcFile) + extraFieldLen;
                fseek(arcFile, extraFieldLen + header->compressSize, SEEK_CUR);
                // pointer on 30 + fileNameLen + extraFieldLen + compressSize
                if (findLast(header->fileName, dirSeparator) == false)
                {
                    headers.push_back(header);
#ifdef DEBUG_ZIP
                    logger->log(" file name: %s",
                        header->fileName.c_str());
                    logger->log(" compression method: %u",
                        CAST_U32(method));
                    logger->log(" compressed size: %u",
                        header->compressSize);
                    logger->log(" uncompressed size: %u",
                        header->uncompressSize);
#endif  // DEBUG_ZIP
                }
                else
                {
#ifdef DEBUG_ZIP
                    logger->log(" dir name: %s",
                        header->fileName.c_str());
#endif  // DEBUG_ZIP
                    dirs.push_back(header->fileName);
                    delete header;
                }
            }
            else if (buf[0] == 0x50 &&
                     buf[1] == 0x4B &&
                     buf[2] == 0x01 &&
                     buf[3] == 0x02)
            {   // central directory file header
                // !!! This is quick way for read zip archives. !!!
                // !!! It ignore modified files in archive. !!!
                // ignoring central directory entries
                break;
            }
            else if (buf[0] == 0x50 &&
                     buf[1] == 0x4B &&
                     buf[2] == 0x05 &&
                     buf[3] == 0x06)
            {   // end of central directory
                // !!! This is quick way for read zip archives. !!!
                // !!! It ignore modified files in archive. !!!
                // ignoring end of central directory
                break;
            }
            else
            {
                reportAlways("Error in header signature (0x%02x%02x%02x%02x)"
                    " in file %s",
                    buf[0],
                    buf[1],
                    buf[2],
                    buf[3],
                    archiveName.c_str());
                delete [] buf;
                fclose(arcFile);
                return false;
            }
        }
        delete [] buf;
        fclose(arcFile);
        return true;
    }

    void reportZlibError(const std::string &text,
                         const int err)
    {
        reportAlways("Zlib error: '%s' in %s",
            text.c_str(),
            getZlibError(err).c_str());
    }

    std::string getZlibError(const int err)
    {
        switch (err)
        {
            case Z_OK:
                return std::string();
            default:
                return "unknown zlib error";
        }
    }

    uint8_t *readCompressedFile(const ZipLocalHeader *restrict const header)
    {
        if (header == nullptr)
        {
            reportAlways("Zip::readCompressedFile: header is null");
            return nullptr;
        }
        FILE *restrict const arcFile = fopen(
            header->zipEntry->root.c_str(),
            "rb");
        if (arcFile == nullptr)
        {
            reportAlways("Can't open zip file %s",
                header->zipEntry->root.c_str());
            return nullptr;
        }

        fseek(arcFile, header->dataOffset, SEEK_SET);
        const uint32_t compressSize = header->compressSize;
        uint8_t *const buf = new uint8_t[compressSize];
        if (fread(static_cast<void*>(buf), 1, compressSize, arcFile) !=
            compressSize)
        {
            reportAlways("Read zip compressed file error from archive: %s",
                header->zipEntry->root.c_str());
            fclose(arcFile);
            delete [] buf;
            return nullptr;
        }
        fclose(arcFile);
        return buf;
    }

    uint8_t *readFile(const ZipLocalHeader *restrict const header)
    {
        if (header == nullptr)
        {
            reportAlways("Open zip file error. header is null.");
            return nullptr;
        }
        uint8_t *restrict const in = readCompressedFile(header);
        if (in == nullptr)
            return nullptr;
        if (header->compressed == false)
            return in;  //  return as is if data not compressed
        const size_t outSize = header->uncompressSize;
        uint8_t *restrict const out = new uint8_t[outSize];
        if (outSize == 0)
            return out;

        z_stream strm;
        strm.zalloc = nullptr;
        strm.zfree = nullptr;
        strm.opaque = nullptr;
        strm.next_in = in;
        strm.avail_in = header->compressSize;
        strm.next_out = out;
        strm.avail_out = outSize;

        int ret = inflateInit2(&strm, -MAX_WBITS);
        if (ret != Z_OK)
        {
            reportZlibError(header->zipEntry->root, ret);
            delete [] in;
            delete [] out;
            return nullptr;
        }
        ret = inflate(&strm, Z_FINISH);
//        ret = inflate(&strm, Z_SYNC_FLUSH);
        if (ret != Z_OK &&
            ret != Z_STREAM_END)
        {
            reportZlibError("file decompression error",
                ret);
            inflateEnd(&strm);
            delete [] in;
            delete [] out;
            return nullptr;
        }
        inflateEnd(&strm);
        delete [] in;
        return out;
    }
}  // namespace Zip
#endif  // USE_PHYSFS