summaryrefslogtreecommitdiff
path: root/src/fs/zip.cpp
blob: 2164a43031413d918dcaa940495530be5567acae (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
/*
 *  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/>.
 */

#include "fs/zip.h"

#include "fs/ziplocalheader.h"

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

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

#include "debug.h"

#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(const std::string &restrict archiveName,
                         std::vector<ZipLocalHeader*> &restrict headers)
    {
        FILE *restrict const arcFile = fopen(archiveName.c_str(),
            "r");
        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;
        ZipLocalHeader *header = nullptr;

        logger->log("Read archive: %s", archiveName.c_str());
        // 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->archiveName = archiveName;
                // skip useless fields
                fseek(arcFile, 14, SEEK_CUR);  // + 14
                // 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));
                header->dataOffset = ftell(arcFile) + extraFieldLen;
                fseek(arcFile, extraFieldLen + header->compressSize, SEEK_CUR);
                // pointer on 30 + fileNameLen + extraFieldLen + compressSize
                if (findLast(header->fileName, "/") == false)
                {
                    headers.push_back(header);
                    logger->log(" file name: %s",
                        header->fileName.c_str());
                    logger->log(" compressed size: %u", header->compressSize);
                    logger->log(" uncompressed size: %u", header->uncompressSize);
                }
            }
            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 because all data already read.
                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 central directory entries because all data already read.
                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;
    }

}  // namespace Zip