summaryrefslogtreecommitdiffstats
path: root/rbutil/rbutilqt/quazip/quazipfileinfo.cpp
blob: d5798ea771ad6fd89cb79c4f697755298af36055 (plain)
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
/*
Copyright (C) 2005-2014 Sergey A. Tachenov

This file is part of QuaZIP.

QuaZIP is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2.1 of the License, or
(at your option) any later version.

QuaZIP 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 Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with QuaZIP.  If not, see <http://www.gnu.org/licenses/>.

See COPYING file for the full LGPL text.

Original ZIP package is copyrighted by Gilles Vollant and contributors,
see quazip/(un)zip.h files for details. Basically it's the zlib license.
*/

#include "quazipfileinfo.h"

#include <QtCore/QDataStream>

static QFile::Permissions permissionsFromExternalAttr(quint32 externalAttr) {
    quint32 uPerm = (externalAttr & 0xFFFF0000u) >> 16;
    QFile::Permissions perm = QFile::Permissions();
    if ((uPerm & 0400) != 0)
        perm |= QFile::ReadOwner;
    if ((uPerm & 0200) != 0)
        perm |= QFile::WriteOwner;
    if ((uPerm & 0100) != 0)
        perm |= QFile::ExeOwner;
    if ((uPerm & 0040) != 0)
        perm |= QFile::ReadGroup;
    if ((uPerm & 0020) != 0)
        perm |= QFile::WriteGroup;
    if ((uPerm & 0010) != 0)
        perm |= QFile::ExeGroup;
    if ((uPerm & 0004) != 0)
        perm |= QFile::ReadOther;
    if ((uPerm & 0002) != 0)
        perm |= QFile::WriteOther;
    if ((uPerm & 0001) != 0)
        perm |= QFile::ExeOther;
    return perm;

}

QFile::Permissions QuaZipFileInfo::getPermissions() const
{
    return permissionsFromExternalAttr(externalAttr);
}

QFile::Permissions QuaZipFileInfo64::getPermissions() const
{
    return permissionsFromExternalAttr(externalAttr);
}

bool QuaZipFileInfo64::toQuaZipFileInfo(QuaZipFileInfo &info) const
{
    bool noOverflow = true;
    info.name = name;
    info.versionCreated = versionCreated;
    info.versionNeeded = versionNeeded;
    info.flags = flags;
    info.method = method;
    info.dateTime = dateTime;
    info.crc = crc;
    if (compressedSize > 0xFFFFFFFFu) {
        info.compressedSize = 0xFFFFFFFFu;
        noOverflow = false;
    } else {
        info.compressedSize = compressedSize;
    }
    if (uncompressedSize > 0xFFFFFFFFu) {
        info.uncompressedSize = 0xFFFFFFFFu;
        noOverflow = false;
    } else {
        info.uncompressedSize = uncompressedSize;
    }
    info.diskNumberStart = diskNumberStart;
    info.internalAttr = internalAttr;
    info.externalAttr = externalAttr;
    info.comment = comment;
    info.extra = extra;
    return noOverflow;
}

static QDateTime getNTFSTime(const QByteArray &extra, int position,
                             int *fineTicks)
{
    QDateTime dateTime;
    QuaExtraFieldHash extraHash = QuaZipFileInfo64::parseExtraField(extra);
    QList<QByteArray> ntfsExtraFields = extraHash[QUAZIP_EXTRA_NTFS_MAGIC];
    if (ntfsExtraFields.isEmpty())
        return dateTime;
    QByteArray ntfsExtraField = ntfsExtraFields.at(0);
    if (ntfsExtraField.length() <= 4)
        return dateTime;
    QByteArray ntfsAttributes = ntfsExtraField.mid(4);
    QuaExtraFieldHash ntfsHash = QuaZipFileInfo64::parseExtraField(ntfsAttributes);
    QList<QByteArray> ntfsTimeAttributes = ntfsHash[QUAZIP_EXTRA_NTFS_TIME_MAGIC];
    if (ntfsTimeAttributes.isEmpty())
        return dateTime;
    QByteArray ntfsTimes = ntfsTimeAttributes.at(0);
    if (ntfsTimes.size() < 24)
        return dateTime;
    QDataStream timeReader(ntfsTimes);
    timeReader.setByteOrder(QDataStream::LittleEndian);
    timeReader.device()->seek(position);
    quint64 time;
    timeReader >> time;
    QDateTime base(QDate(1601, 1, 1), QTime(0, 0), Qt::UTC);
    dateTime = base.addMSecs(time / 10000);
    if (fineTicks != NULL) {
        *fineTicks = static_cast<int>(time % 10000);
    }
    return dateTime;
}

QDateTime QuaZipFileInfo64::getNTFSmTime(int *fineTicks) const
{
    return getNTFSTime(extra, 0, fineTicks);
}

QDateTime QuaZipFileInfo64::getNTFSaTime(int *fineTicks) const
{
    return getNTFSTime(extra, 8, fineTicks);
}

QDateTime QuaZipFileInfo64::getNTFScTime(int *fineTicks) const
{
    return getNTFSTime(extra, 16, fineTicks);
}

QDateTime QuaZipFileInfo64::getExtTime(const QByteArray &extra, int flag)
{
    QDateTime dateTime;
    QuaExtraFieldHash extraHash = QuaZipFileInfo64::parseExtraField(extra);
    QList<QByteArray> extTimeFields = extraHash[QUAZIP_EXTRA_EXT_TIME_MAGIC];
    if (extTimeFields.isEmpty())
        return dateTime;
    QByteArray extTimeField = extTimeFields.at(0);
    if (extTimeField.length() < 1)
        return dateTime;
    QDataStream input(extTimeField);
    input.setByteOrder(QDataStream::LittleEndian);
    quint8 flags;
    input >> flags;
    int flagsRemaining = flags;
    while (!input.atEnd()) {
        int nextFlag = flagsRemaining & -flagsRemaining;
        flagsRemaining &= flagsRemaining - 1;
        qint32 time;
        input >> time;
        if (nextFlag == flag) {
            QDateTime base(QDate(1970, 1, 1), QTime(0, 0), Qt::UTC);
            dateTime = base.addSecs(time);
            return dateTime;
        }
    }
    return dateTime;
}

QDateTime QuaZipFileInfo64::getExtModTime() const
{
    return getExtTime(extra, 1);
}

QuaExtraFieldHash QuaZipFileInfo64::parseExtraField(const QByteArray &extraField)
{
    QDataStream input(extraField);
    input.setByteOrder(QDataStream::LittleEndian);
    QHash<quint16, QList<QByteArray> > result;
    while (!input.atEnd()) {
        quint16 id, size;
        input >> id;
        if (input.status() == QDataStream::ReadPastEnd)
            return result;
        input >> size;
        if (input.status() == QDataStream::ReadPastEnd)
            return result;
        QByteArray data;
        data.resize(size);
        int read = input.readRawData(data.data(), data.size());
        if (read < data.size())
            return result;
        result[id] << data;
    }
    return result;
}