Skip to content

Commit

Permalink
Lint code. Remove bad allocation hidden errors
Browse files Browse the repository at this point in the history
  • Loading branch information
myst6re committed Mar 23, 2021
1 parent ea491c2 commit a2712d2
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 107 deletions.
81 changes: 2 additions & 79 deletions lzs/LZS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,84 +32,14 @@ qint32 LZS::dad[4097];
unsigned char LZS::text_buf[4113];
QByteArray LZS::result;

const QByteArray &LZS::decompress(const QByteArray &data, int max, LZSObserver *observer)
{
return decompress(data.constData(), data.size(), max, observer);
}

const QByteArray &LZS::decompress(const char *data, int fileSize, int max, LZSObserver *observer)
{
int curResult = 0, sizeAlloc = max + 10;
quint16 curBuff = 4078, offset, flagByte = 0, i, length;
const quint8 *fileData = (const quint8 *)data,
*endFileData = fileData + fileSize;

if (observer) {
observer->setMaximum(fileSize);
}

// Impossible case
if(quint64(sizeAlloc) > 2000 * quint64(fileSize)) {
qWarning() << "LZS::decompress impossible ratio case" << sizeAlloc << 2000 * quint64(fileSize);
result.clear();
return result;
}

// Reduce the amount of realloc
if (result.size() < sizeAlloc) {
try {
result.resize(sizeAlloc);
} catch(std::bad_alloc) {
result.clear();
return result;
}
}

memset(text_buf, 0, 4078); // The buffer of 4096 bytes is set to 0

forever {
if (((flagByte >>= 1) & 256) == 0) {
flagByte = *fileData++ | 0xff00; // First byte
}

if (observer) { // Output
observer->setValue(fileData - (const quint8 *)data);
}

if (fileData >= endFileData || curResult >= max) {
result.truncate(curResult);
return result; // Ending
}

if (flagByte & 1) {
// Uncompressed byte
result[curResult] = text_buf[curBuff] = *fileData++;
curBuff = (curBuff + 1) & 4095;
++curResult;
} else {
// Infos to retrieve uncompressed data
offset = *fileData++;
length = *fileData++;
offset |= (length & 0xF0) << 4;
length = (length & 0xF) + 2 + offset;

for (i = offset; i <= length; ++i) {
result[curResult] = text_buf[curBuff] = text_buf[i & 4095];
curBuff = (curBuff + 1) & 4095;
++curResult;
}
}
}
}

const QByteArray &LZS::decompressAll(const QByteArray &data, LZSObserver *observer)
{
return decompressAll(data.constData(), data.size(), observer);
}

const QByteArray &LZS::decompressAll(const char *data, int fileSize, LZSObserver *observer)
{
int curResult = 0, sizeAlloc = fileSize * 5;
int curResult = 0, sizeAlloc = int(qMin(quint64(fileSize * 5), quint64(INT_MAX)));
quint16 curBuff = 4078, offset, flagByte = 0, i, length;
const quint8 *fileData = (const quint8 *)data,
*endFileData = fileData + fileSize;
Expand All @@ -118,19 +48,12 @@ const QByteArray &LZS::decompressAll(const char *data, int fileSize, LZSObserver
observer->setMaximum(fileSize);
}

// Impossible case
if(sizeAlloc > 2000 * fileSize) {
result.clear();
return result;
}

// Reduce the amount of realloc
if (result.size() < sizeAlloc) {
try {
result.resize(sizeAlloc);
} catch(std::bad_alloc) {
result.clear();
return result;
// Ignore, try anyway
}
}

Expand Down
10 changes: 4 additions & 6 deletions lzs/LZS.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@
class LZS
{
public:
static const QByteArray &decompress(const QByteArray &fileData, int max, LZSObserver *observer = NULL);
static const QByteArray &decompress(const char *data, int fileSize, int max, LZSObserver *observer = NULL);
static const QByteArray &decompressAll(const QByteArray &fileData, LZSObserver *observer = NULL);
static const QByteArray &decompressAll(const char *data, int fileSize, LZSObserver *observer = NULL);
static const QByteArray &compress(const QByteArray &fileData, LZSObserver *observer = NULL);
static const QByteArray &compress(const char *data, int sizeData, LZSObserver *observer = NULL);
static const QByteArray &decompressAll(const QByteArray &fileData, LZSObserver *observer = nullptr);
static const QByteArray &decompressAll(const char *data, int fileSize, LZSObserver *observer = nullptr);
static const QByteArray &compress(const QByteArray &fileData, LZSObserver *observer = nullptr);
static const QByteArray &compress(const char *data, int sizeData, LZSObserver *observer = nullptr);

private:
static void InsertNode(qint32 r);
Expand Down
14 changes: 1 addition & 13 deletions lzs/LZSObserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,15 @@
#include "LZSObserver.h"
#include <stdio.h>

LZSObserver::LZSObserver()
{
}

LZSObserverPercent::LZSObserverPercent()
{
}

void LZSObserverPercent::setValue(int value)
{
int percent = value * 100.0 / (double)maximum();
int percent = int(qint64(value * 100) / maximum());
if (percent != _lastPercent) {
_lastPercent = percent;
setPercent(percent);
}
}

LZSObserverStdOut::LZSObserverStdOut()
{
}

void LZSObserverStdOut::setPercent(int percent)
{
printf("[%d%%] %s\r", percent, qPrintable(_filename));
Expand Down
17 changes: 10 additions & 7 deletions lzs/LZSObserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
class LZSObserver
{
public:
LZSObserver();
inline LZSObserver() {}
inline virtual ~LZSObserver() {}

inline void setMaximum(int maximum) {
_maximum = maximum;
Expand All @@ -32,29 +33,31 @@ class LZSObserver
return _maximum;
}

virtual void setValue(int value)=0;
virtual void setValue(int value) = 0;
private:
int _maximum;
};

class LZSObserverPercent : public LZSObserver
{
public:
LZSObserverPercent();
void setValue(int value);
virtual void setPercent(int percent)=0;
inline LZSObserverPercent() {}
inline virtual ~LZSObserverPercent() override {}
virtual void setValue(int value) override;
virtual void setPercent(int percent) = 0;
private:
int _lastPercent;
};

class LZSObserverStdOut : public LZSObserverPercent
{
public:
LZSObserverStdOut();
inline LZSObserverStdOut() {}
inline virtual ~LZSObserverStdOut() override {}
inline void setFilename(const QString &filename) {
_filename = filename;
}
virtual void setPercent(int percent);
virtual void setPercent(int percent) override;
private:
QString _filename;
};
Expand Down
4 changes: 2 additions & 2 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ int main(int argc, char *argv[])
#endif

Arguments args;
quint32 lzsSize;
qint32 lzsSize = 0;
LZSObserver *observer;
LZSObserverStdOut stdObserver;

if (args.quiet()) {
observer = NULL;
observer = nullptr;
} else {
observer = &stdObserver;
}
Expand Down

0 comments on commit a2712d2

Please sign in to comment.