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 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
/*
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 "quaziodevice.h"
#define QUAZIO_INBUFSIZE 4096
#define QUAZIO_OUTBUFSIZE 4096
/// \cond internal
class QuaZIODevicePrivate {
friend class QuaZIODevice;
QuaZIODevicePrivate(QIODevice *io, QuaZIODevice *q);
~QuaZIODevicePrivate();
QIODevice *io;
QuaZIODevice *q;
z_stream zins;
z_stream zouts;
char *inBuf{nullptr};
int inBufPos{0};
int inBufSize{0};
char *outBuf{nullptr};
int outBufPos{0};
int outBufSize{0};
bool zBufError{false};
bool atEnd{false};
bool flush(int sync);
int doFlush(QString &error);
};
QuaZIODevicePrivate::QuaZIODevicePrivate(QIODevice *io, QuaZIODevice *q):
io(io),
q(q)
{
zins.zalloc = (alloc_func) nullptr;
zins.zfree = (free_func) nullptr;
zins.opaque = nullptr;
zouts.zalloc = (alloc_func) nullptr;
zouts.zfree = (free_func) nullptr;
zouts.opaque = nullptr;
inBuf = new char[QUAZIO_INBUFSIZE];
outBuf = new char[QUAZIO_OUTBUFSIZE];
#ifdef QUAZIP_ZIODEVICE_DEBUG_OUTPUT
debug.setFileName("debug.out");
debug.open(QIODevice::WriteOnly);
#endif
#ifdef QUAZIP_ZIODEVICE_DEBUG_INPUT
indebug.setFileName("debug.in");
indebug.open(QIODevice::WriteOnly);
#endif
}
QuaZIODevicePrivate::~QuaZIODevicePrivate()
{
#ifdef QUAZIP_ZIODEVICE_DEBUG_OUTPUT
debug.close();
#endif
#ifdef QUAZIP_ZIODEVICE_DEBUG_INPUT
indebug.close();
#endif
delete[] inBuf;
delete[] outBuf;
}
bool QuaZIODevicePrivate::flush(int sync)
{
QString error;
if (doFlush(error) < 0) {
q->setErrorString(error);
return false;
}
// can't flush buffer, some data is still waiting
if (outBufPos < outBufSize)
return true;
Bytef c = 0;
zouts.next_in = &c; // fake input buffer
zouts.avail_in = 0; // of zero size
do {
zouts.next_out = reinterpret_cast<Bytef *>(outBuf);
zouts.avail_out = QUAZIO_OUTBUFSIZE;
int result = deflate(&zouts, sync);
switch (result) {
case Z_OK:
case Z_STREAM_END:
outBufSize = reinterpret_cast<char *>(zouts.next_out) - outBuf;
if (doFlush(error) < 0) {
q->setErrorString(error);
return false;
}
if (outBufPos < outBufSize)
return true;
break;
case Z_BUF_ERROR: // nothing to write?
return true;
default:
q->setErrorString(QString::fromLocal8Bit(zouts.msg));
return false;
}
} while (zouts.avail_out == 0);
return true;
}
int QuaZIODevicePrivate::doFlush(QString &error)
{
int flushed = 0;
while (outBufPos < outBufSize) {
int more = io->write(outBuf + outBufPos, outBufSize - outBufPos);
if (more == -1) {
error = io->errorString();
return -1;
}
if (more == 0)
break;
outBufPos += more;
flushed += more;
}
if (outBufPos == outBufSize) {
outBufPos = outBufSize = 0;
}
return flushed;
}
/// \endcond
// #define QUAZIP_ZIODEVICE_DEBUG_OUTPUT
// #define QUAZIP_ZIODEVICE_DEBUG_INPUT
#ifdef QUAZIP_ZIODEVICE_DEBUG_OUTPUT
#include <QtCore/QFile>
static QFile debug;
#endif
#ifdef QUAZIP_ZIODEVICE_DEBUG_INPUT
#include <QtCore/QFile>
static QFile indebug;
#endif
QuaZIODevice::QuaZIODevice(QIODevice *io, QObject *parent):
QIODevice(parent),
d(new QuaZIODevicePrivate(io, this))
{
connect(io, SIGNAL(readyRead()), SIGNAL(readyRead()));
}
QuaZIODevice::~QuaZIODevice()
{
if (isOpen())
close();
delete d;
}
QIODevice *QuaZIODevice::getIoDevice() const
{
return d->io;
}
bool QuaZIODevice::open(QIODevice::OpenMode mode)
{
if ((mode & QIODevice::Append) != 0) {
setErrorString(tr("QIODevice::Append is not supported for"
" QuaZIODevice"));
return false;
}
if ((mode & QIODevice::ReadWrite) == QIODevice::ReadWrite) {
setErrorString(tr("QIODevice::ReadWrite is not supported for"
" QuaZIODevice"));
return false;
}
if ((mode & QIODevice::ReadOnly) != 0) {
if (inflateInit(&d->zins) != Z_OK) {
setErrorString(QString::fromLocal8Bit(d->zins.msg));
return false;
}
}
if ((mode & QIODevice::WriteOnly) != 0) {
if (deflateInit(&d->zouts, Z_DEFAULT_COMPRESSION) != Z_OK) {
setErrorString(QString::fromLocal8Bit(d->zouts.msg));
return false;
}
}
return QIODevice::open(mode);
}
void QuaZIODevice::close()
{
if ((openMode() & QIODevice::ReadOnly) != 0) {
if (inflateEnd(&d->zins) != Z_OK) {
setErrorString(QString::fromLocal8Bit(d->zins.msg));
}
}
if ((openMode() & QIODevice::WriteOnly) != 0) {
d->flush(Z_FINISH);
if (deflateEnd(&d->zouts) != Z_OK) {
setErrorString(QString::fromLocal8Bit(d->zouts.msg));
}
}
QIODevice::close();
}
qint64 QuaZIODevice::readData(char *data, qint64 maxSize)
{
int read = 0;
while (read < maxSize) {
if (d->inBufPos == d->inBufSize) {
d->inBufPos = 0;
d->inBufSize = d->io->read(d->inBuf, QUAZIO_INBUFSIZE);
if (d->inBufSize == -1) {
d->inBufSize = 0;
setErrorString(d->io->errorString());
return -1;
}
if (d->inBufSize == 0)
break;
}
while (read < maxSize && d->inBufPos < d->inBufSize) {
d->zins.next_in = reinterpret_cast<Bytef *>(d->inBuf + d->inBufPos);
d->zins.avail_in = d->inBufSize - d->inBufPos;
d->zins.next_out = reinterpret_cast<Bytef *>(data + read);
d->zins.avail_out = static_cast<uInt>(maxSize - read); // hope it's less than 2GB
int more = 0;
switch (inflate(&d->zins, Z_SYNC_FLUSH)) {
case Z_OK:
read = reinterpret_cast<char *>(d->zins.next_out) - data;
d->inBufPos = reinterpret_cast<char *>(d->zins.next_in) - d->inBuf;
break;
case Z_STREAM_END:
read = reinterpret_cast<char *>(d->zins.next_out) - data;
d->inBufPos = reinterpret_cast<char *>(d->zins.next_in) - d->inBuf;
d->atEnd = true;
return read;
case Z_BUF_ERROR: // this should never happen, but just in case
if (!d->zBufError) {
qWarning("Z_BUF_ERROR detected with %d/%d in/out, weird",
d->zins.avail_in, d->zins.avail_out);
d->zBufError = true;
}
memmove(d->inBuf, d->inBuf + d->inBufPos, d->inBufSize - d->inBufPos);
d->inBufSize -= d->inBufPos;
d->inBufPos = 0;
more = d->io->read(d->inBuf + d->inBufSize, QUAZIO_INBUFSIZE - d->inBufSize);
if (more == -1) {
setErrorString(d->io->errorString());
return -1;
}
if (more == 0)
return read;
d->inBufSize += more;
break;
default:
setErrorString(QString::fromLocal8Bit(d->zins.msg));
return -1;
}
}
}
#ifdef QUAZIP_ZIODEVICE_DEBUG_INPUT
indebug.write(data, read);
#endif
return read;
}
qint64 QuaZIODevice::writeData(const char *data, qint64 maxSize)
{
int written = 0;
QString error;
if (d->doFlush(error) == -1) {
setErrorString(error);
return -1;
}
while (written < maxSize) {
// there is some data waiting in the output buffer
if (d->outBufPos < d->outBufSize)
return written;
d->zouts.next_in = (Bytef *) (data + written);
d->zouts.avail_in = static_cast<uInt>(maxSize - written); // hope it's less than 2GB
d->zouts.next_out = reinterpret_cast<Bytef *>(d->outBuf);
d->zouts.avail_out = QUAZIO_OUTBUFSIZE;
switch (deflate(&d->zouts, Z_NO_FLUSH)) {
case Z_OK:
written = reinterpret_cast<char *>(d->zouts.next_in) - data;
d->outBufSize = reinterpret_cast<char *>(d->zouts.next_out) - d->outBuf;
break;
default:
setErrorString(QString::fromLocal8Bit(d->zouts.msg));
return -1;
}
if (d->doFlush(error) == -1) {
setErrorString(error);
return -1;
}
}
#ifdef QUAZIP_ZIODEVICE_DEBUG_OUTPUT
debug.write(data, written);
#endif
return written;
}
bool QuaZIODevice::flush()
{
return d->flush(Z_SYNC_FLUSH);
}
bool QuaZIODevice::isSequential() const
{
return true;
}
bool QuaZIODevice::atEnd() const
{
// Here we MUST check QIODevice::bytesAvailable() because WE
// might have reached the end, but QIODevice didn't--
// it could have simply pre-buffered all remaining data.
return (openMode() == NotOpen) || (QIODevice::bytesAvailable() == 0 && d->atEnd);
}
qint64 QuaZIODevice::bytesAvailable() const
{
// If we haven't recevied Z_STREAM_END, it means that
// we have at least one more input byte available.
// Plus whatever QIODevice has buffered.
return (d->atEnd ? 0 : 1) + QIODevice::bytesAvailable();
}
|