-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathZipArchive.cpp
More file actions
297 lines (257 loc) · 10.2 KB
/
Copy pathZipArchive.cpp
File metadata and controls
297 lines (257 loc) · 10.2 KB
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
#include "ZipArchive.h"
#include <zlib.h>
#include <algorithm>
#include <limits>
namespace AetherSDR {
namespace {
void setZipError(QString* error, const QString& message)
{
if (error)
*error = message;
}
quint16 readLe16(const QByteArray& bytes, qsizetype offset)
{
if (offset < 0 || offset + 2 > bytes.size())
return 0;
const auto* p = reinterpret_cast<const uchar*>(bytes.constData() + offset);
return quint16(p[0]) | (quint16(p[1]) << 8);
}
quint32 readLe32(const QByteArray& bytes, qsizetype offset)
{
if (offset < 0 || offset + 4 > bytes.size())
return 0;
const auto* p = reinterpret_cast<const uchar*>(bytes.constData() + offset);
return quint32(p[0]) | (quint32(p[1]) << 8) | (quint32(p[2]) << 16) | (quint32(p[3]) << 24);
}
void appendLe16(QByteArray& out, quint16 value)
{
out.append(char(value & 0xff));
out.append(char((value >> 8) & 0xff));
}
void appendLe32(QByteArray& out, quint32 value)
{
out.append(char(value & 0xff));
out.append(char((value >> 8) & 0xff));
out.append(char((value >> 16) & 0xff));
out.append(char((value >> 24) & 0xff));
}
bool inflateRawDeflate(const QByteArray& compressed, qsizetype expectedSize,
QByteArray* out, QString* error)
{
if (expectedSize < 0) {
setZipError(error, QStringLiteral("Invalid ZIP entry size."));
return false;
}
out->resize(expectedSize);
z_stream stream{};
stream.next_in = reinterpret_cast<Bytef*>(const_cast<char*>(compressed.constData()));
stream.avail_in = static_cast<uInt>(compressed.size());
stream.next_out = reinterpret_cast<Bytef*>(out->data());
stream.avail_out = static_cast<uInt>(out->size());
int rc = inflateInit2(&stream, -MAX_WBITS);
if (rc != Z_OK) {
setZipError(error, QStringLiteral("Could not initialize ZIP inflater."));
return false;
}
rc = inflate(&stream, Z_FINISH);
inflateEnd(&stream);
if (rc != Z_STREAM_END || stream.total_out != static_cast<uLong>(expectedSize)) {
setZipError(error, QStringLiteral("Could not decompress ZIP entry."));
return false;
}
return true;
}
} // namespace
QMap<QString, QByteArray> readZipEntries(const QByteArray& zip, QString* error)
{
QMap<QString, QByteArray> entries;
if (zip.size() < 22 || !zip.startsWith("PK")) {
setZipError(error, QStringLiteral("Package is not a ZIP-format .ssdr_cfg file."));
return entries;
}
const qsizetype minEocd = 22;
const qsizetype searchStart = std::max<qsizetype>(0, zip.size() - 65557);
qsizetype eocd = -1;
for (qsizetype i = zip.size() - minEocd; i >= searchStart; --i) {
if (readLe32(zip, i) == 0x06054b50) {
eocd = i;
break;
}
if (i == 0)
break;
}
if (eocd < 0) {
setZipError(error, QStringLiteral("Could not find ZIP central directory."));
return entries;
}
const quint16 entryCount = readLe16(zip, eocd + 10);
const quint32 centralDirSize = readLe32(zip, eocd + 12);
const quint32 centralDirOffset = readLe32(zip, eocd + 16);
if (centralDirOffset + centralDirSize > quint32(zip.size())) {
setZipError(error, QStringLiteral("ZIP central directory is outside the package."));
return entries;
}
qsizetype offset = centralDirOffset;
for (quint16 i = 0; i < entryCount; ++i) {
if (offset + 46 > zip.size() || readLe32(zip, offset) != 0x02014b50) {
setZipError(error, QStringLiteral("Invalid ZIP central directory entry."));
entries.clear();
return entries;
}
const quint16 method = readLe16(zip, offset + 10);
const quint32 expectedCrc = readLe32(zip, offset + 16);
const quint32 compressedSize = readLe32(zip, offset + 20);
const quint32 uncompressedSize = readLe32(zip, offset + 24);
const quint16 nameLen = readLe16(zip, offset + 28);
const quint16 extraLen = readLe16(zip, offset + 30);
const quint16 commentLen = readLe16(zip, offset + 32);
const quint32 localOffset = readLe32(zip, offset + 42);
if (offset + 46 + nameLen + extraLen + commentLen > zip.size()
|| localOffset + 30 > quint32(zip.size())
|| readLe32(zip, localOffset) != 0x04034b50) {
setZipError(error, QStringLiteral("Invalid ZIP entry header."));
entries.clear();
return entries;
}
const QString name = QString::fromUtf8(zip.constData() + offset + 46, nameLen);
const quint16 localNameLen = readLe16(zip, localOffset + 26);
const quint16 localExtraLen = readLe16(zip, localOffset + 28);
const quint32 dataOffset = localOffset + 30 + localNameLen + localExtraLen;
if (dataOffset + compressedSize > quint32(zip.size())) {
setZipError(error, QStringLiteral("ZIP entry data is outside the package."));
entries.clear();
return entries;
}
const QByteArray compressed = zip.mid(dataOffset, compressedSize);
QByteArray data;
if (method == 0) {
data = compressed;
} else if (method == 8) {
if (!inflateRawDeflate(compressed, uncompressedSize, &data, error)) {
entries.clear();
return entries;
}
} else {
setZipError(error, QStringLiteral("Unsupported ZIP compression method %1.").arg(method));
entries.clear();
return entries;
}
const quint32 actualCrc = crc32(0L, reinterpret_cast<const Bytef*>(data.constData()),
static_cast<uInt>(data.size()));
if (actualCrc != expectedCrc) {
setZipError(error, QStringLiteral("ZIP entry checksum mismatch."));
entries.clear();
return entries;
}
entries.insert(name, data);
offset += 46 + nameLen + extraLen + commentLen;
}
return entries;
}
namespace {
// Raw DEFLATE (no zlib header), matching the readZipEntries inflate path
// (inflateInit2(-MAX_WBITS)) so the writer and reader agree on the framing.
bool rawDeflate(const QByteArray& in, QByteArray& out)
{
z_stream z{};
if (deflateInit2(&z, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -MAX_WBITS, 8,
Z_DEFAULT_STRATEGY) != Z_OK) {
return false;
}
out.resize(static_cast<int>(deflateBound(&z, static_cast<uLong>(in.size()))));
z.next_in = reinterpret_cast<Bytef*>(const_cast<char*>(in.constData()));
z.avail_in = static_cast<uInt>(in.size());
z.next_out = reinterpret_cast<Bytef*>(out.data());
z.avail_out = static_cast<uInt>(out.size());
const int rc = deflate(&z, Z_FINISH);
const uLong produced = z.total_out;
deflateEnd(&z);
if (rc != Z_STREAM_END)
return false;
out.resize(static_cast<int>(produced));
return true;
}
// Shared ZIP writer. compress=false → stored (method 0); compress=true →
// raw-deflate (method 8). Headers carry compressed + uncompressed sizes
// independently; the CRC is always over the uncompressed data.
QByteArray writeZipImpl(const QList<ZipEntryData>& entries, bool compress)
{
if (entries.size() > std::numeric_limits<quint16>::max())
return {};
QByteArray out;
QByteArray centralDir;
for (const ZipEntryData& entry : entries) {
const QByteArray name = entry.name.toUtf8();
const QByteArray& raw = entry.data;
QByteArray deflated;
if (compress && !rawDeflate(raw, deflated))
return {};
const QByteArray& payload = compress ? deflated : raw;
const quint16 method = compress ? quint16(8) : quint16(0);
if (name.size() > std::numeric_limits<quint16>::max()
|| raw.size() > std::numeric_limits<quint32>::max()
|| payload.size() > std::numeric_limits<quint32>::max()
|| out.size() > std::numeric_limits<quint32>::max()) {
return {};
}
const quint32 localOffset = static_cast<quint32>(out.size());
const quint32 checksum = crc32(0L, reinterpret_cast<const Bytef*>(raw.constData()),
static_cast<uInt>(raw.size()));
appendLe32(out, 0x04034b50);
appendLe16(out, 20);
appendLe16(out, 0);
appendLe16(out, method);
appendLe16(out, 0);
appendLe16(out, 0);
appendLe32(out, checksum);
appendLe32(out, static_cast<quint32>(payload.size()));
appendLe32(out, static_cast<quint32>(raw.size()));
appendLe16(out, static_cast<quint16>(name.size()));
appendLe16(out, 0);
out += name;
out += payload;
appendLe32(centralDir, 0x02014b50);
appendLe16(centralDir, 20);
appendLe16(centralDir, 20);
appendLe16(centralDir, 0);
appendLe16(centralDir, method);
appendLe16(centralDir, 0);
appendLe16(centralDir, 0);
appendLe32(centralDir, checksum);
appendLe32(centralDir, static_cast<quint32>(payload.size()));
appendLe32(centralDir, static_cast<quint32>(raw.size()));
appendLe16(centralDir, static_cast<quint16>(name.size()));
appendLe16(centralDir, 0);
appendLe16(centralDir, 0);
appendLe16(centralDir, 0);
appendLe16(centralDir, 0);
appendLe32(centralDir, 0);
appendLe32(centralDir, localOffset);
centralDir += name;
}
if (centralDir.size() > std::numeric_limits<quint32>::max()
|| out.size() > std::numeric_limits<quint32>::max()) {
return {};
}
const quint32 centralDirOffset = static_cast<quint32>(out.size());
out += centralDir;
appendLe32(out, 0x06054b50);
appendLe16(out, 0);
appendLe16(out, 0);
appendLe16(out, static_cast<quint16>(entries.size()));
appendLe16(out, static_cast<quint16>(entries.size()));
appendLe32(out, static_cast<quint32>(centralDir.size()));
appendLe32(out, centralDirOffset);
appendLe16(out, 0);
return out;
}
} // namespace
QByteArray writeStoredZip(const QList<ZipEntryData>& entries)
{
return writeZipImpl(entries, false);
}
QByteArray writeDeflatedZip(const QList<ZipEntryData>& entries)
{
return writeZipImpl(entries, true);
}
} // namespace AetherSDR