Skip to content

Commit

Permalink
ACCESS: fix valgrind errors in decompressor
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Kiewitz committed Jul 5, 2015
1 parent f4ee839 commit 83f4565
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
44 changes: 33 additions & 11 deletions engines/access/decompress.cpp
Expand Up @@ -46,7 +46,7 @@ void LzwDecompressor::decompress(byte *source, byte *dest) {
maxCodeValue = 512;

copyLength = 0;
_bitPos = 0;
_sourceBitsLeft = 8;

while (1) {

Expand Down Expand Up @@ -97,17 +97,39 @@ uint16 LzwDecompressor::getCode() {
const byte bitMasks[9] = {
0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0x0FF
};
uint16 bits, loCode, hiCode;
loCode = (READ_LE_UINT16(_source) >> _bitPos) & 0xFF;
_source++;
bits = _codeLength - 8;
hiCode = (READ_LE_UINT16(_source) >> _bitPos) & bitMasks[bits];
_bitPos += bits;
if (_bitPos > 8) {
_source++;
_bitPos -= 8;

byte resultBitsLeft = _codeLength;
byte resultBitsPos = 0;
uint16 result = 0;
byte currentByte = *_source;
byte currentBits = 0;

// Get bits of current byte
while (resultBitsLeft) {
if (resultBitsLeft < _sourceBitsLeft) {
// we need less than we have left
currentBits = (currentByte >> (8 - _sourceBitsLeft)) & bitMasks[resultBitsLeft];
result |= (currentBits << resultBitsPos);
_sourceBitsLeft -= resultBitsLeft;
resultBitsLeft = 0;

} else {
// we need as much as we have left or more
resultBitsLeft -= _sourceBitsLeft;
currentBits = currentByte >> (8 - _sourceBitsLeft);
result |= (currentBits << resultBitsPos);
resultBitsPos += _sourceBitsLeft;

// Go to next byte
_source++;

_sourceBitsLeft = 8;
if (resultBitsLeft) {
currentByte = *_source;
}
}
}
return (hiCode << 8) | loCode;
return result;
}

uint32 decompressDBE(byte *source, byte **dest) {
Expand Down
3 changes: 2 additions & 1 deletion engines/access/decompress.h
Expand Up @@ -32,7 +32,8 @@ class LzwDecompressor {
void decompress(byte *source, byte *dest);
private:
byte *_source;
byte _codeLength, _bitPos;
byte _sourceBitsLeft;
byte _codeLength;
uint16 getCode();
};

Expand Down

0 comments on commit 83f4565

Please sign in to comment.