Cleanup qUncompress
Cleanup the code in qUncompress and make use of QArrayDataPointer to keep track of memory. Change-Id: I2c11f0468813698d2b7c25acd0f8786a289739a0 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
88c72d972a
commit
b800f3039a
@ -692,14 +692,6 @@ QByteArray qCompress(const uchar* data, int nbytes, int compressionLevel)
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef QT_NO_COMPRESS
|
#ifndef QT_NO_COMPRESS
|
||||||
namespace {
|
|
||||||
struct QByteArrayDataDeleter
|
|
||||||
{
|
|
||||||
static inline void cleanup(QTypedArrayData<char> *d)
|
|
||||||
{ if (d) QTypedArrayData<char>::deallocate(d); }
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
static QByteArray invalidCompressedData()
|
static QByteArray invalidCompressedData()
|
||||||
{
|
{
|
||||||
qWarning("qUncompress: Input data is corrupted");
|
qWarning("qUncompress: Input data is corrupted");
|
||||||
@ -733,24 +725,22 @@ QByteArray qUncompress(const uchar* data, int nbytes)
|
|||||||
return invalidCompressedData();
|
return invalidCompressedData();
|
||||||
}
|
}
|
||||||
|
|
||||||
QPair<QByteArray::Data *, char *> pair = QByteArray::Data::allocate(expectedSize + 1);
|
QByteArray::DataPointer d(QByteArray::Data::allocate(expectedSize + 1));
|
||||||
QScopedPointer<QByteArray::Data, QByteArrayDataDeleter> d(pair.first);
|
|
||||||
if (Q_UNLIKELY(d.data() == nullptr))
|
if (Q_UNLIKELY(d.data() == nullptr))
|
||||||
return invalidCompressedData();
|
return invalidCompressedData();
|
||||||
|
|
||||||
forever {
|
forever {
|
||||||
ulong alloc = len;
|
ulong alloc = len;
|
||||||
|
int res = ::uncompress((uchar*)d.data(), &len,
|
||||||
int res = ::uncompress((uchar*)pair.second, &len,
|
|
||||||
data+4, nbytes-4);
|
data+4, nbytes-4);
|
||||||
|
|
||||||
switch (res) {
|
switch (res) {
|
||||||
case Z_OK: {
|
case Z_OK: {
|
||||||
Q_ASSERT(len <= alloc);
|
Q_ASSERT(len <= alloc);
|
||||||
Q_UNUSED(alloc);
|
Q_UNUSED(alloc)
|
||||||
QByteArray::DataPointer dataPtr = { d.take(), pair.second, uint(len) };
|
d.data()[len] = '\0';
|
||||||
pair.second[len] = '\0';
|
d.size = len;
|
||||||
return QByteArray(dataPtr);
|
return QByteArray(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
case Z_MEM_ERROR:
|
case Z_MEM_ERROR:
|
||||||
@ -764,12 +754,9 @@ QByteArray qUncompress(const uchar* data, int nbytes)
|
|||||||
return invalidCompressedData();
|
return invalidCompressedData();
|
||||||
} else {
|
} else {
|
||||||
// grow the block
|
// grow the block
|
||||||
pair = QByteArray::Data::reallocateUnaligned(d.data(), pair.second, len + 1);
|
d->reallocate(d->allocatedCapacity()*2, QByteArray::Data::GrowsForward);
|
||||||
Q_CHECK_PTR(pair.first);
|
if (Q_UNLIKELY(d.data() == nullptr))
|
||||||
if (Q_UNLIKELY(pair.first == nullptr))
|
|
||||||
return invalidCompressedData();
|
return invalidCompressedData();
|
||||||
d.take(); // don't free
|
|
||||||
d.reset(pair.first);
|
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user