* Fix commit

* Fix LZO implementation (use lzo1x_decompress_safe)
This commit is contained in:
Phillip Stephens 2015-03-01 13:22:59 -08:00
parent c28cfbaba6
commit 190f1e8c17
5 changed files with 53 additions and 53 deletions

View File

@ -10,7 +10,7 @@ namespace io
class IStreamReader : public IStream class IStreamReader : public IStream
{ {
public: public:
virtual ~IStreamReader(); virtual ~IStreamReader() {}
virtual void setEndian(Endian) = 0; virtual void setEndian(Endian) = 0;
virtual Endian endian() const= 0; virtual Endian endian() const= 0;
virtual bool isBigEndian() const= 0; virtual bool isBigEndian() const= 0;

View File

@ -10,7 +10,7 @@ namespace io
class IStreamWriter : public IStream class IStreamWriter : public IStream
{ {
public: public:
virtual ~IStreamWriter(); virtual ~IStreamWriter() {}
virtual void setEndian(Endian) = 0; virtual void setEndian(Endian) = 0;
virtual Endian endian() const= 0; virtual Endian endian() const= 0;
virtual bool isBigEndian() const= 0; virtual bool isBigEndian() const= 0;

View File

@ -16,7 +16,7 @@
#ifndef MEMORYWRITER_HPP #ifndef MEMORYWRITER_HPP
#define MEMORYWRITER_HPP #define MEMORYWRITER_HPP
#include "Athena/IStream.hpp" #include "Athena/IStreamWriter.hpp"
#include <string> #include <string>
#include <functional> #include <functional>
@ -33,7 +33,7 @@ namespace io
* this allows for fast, flexible code as well as the ability to quickly modify data * this allows for fast, flexible code as well as the ability to quickly modify data
* \sa Stream * \sa Stream
*/ */
class MemoryWriter : public IStream class MemoryWriter : public IStreamWriter
{ {
public: public:
/*! \brief This constructor takes an existing buffer to write to. /*! \brief This constructor takes an existing buffer to write to.

View File

@ -109,7 +109,7 @@ atInt32 decompressLZO(const atUint8* source, const atInt32 sourceSize, atUint8*
{ {
int srcSize = sourceSize; int srcSize = sourceSize;
int size = dstSize; int size = dstSize;
int result = lzo1x_decompress(source, srcSize, dst, (lzo_uintp)&size, NULL); int result = lzo1x_decompress_safe(source, srcSize, dst, (lzo_uintp)&size, NULL);
dstSize -= size; dstSize -= size;
return result; return result;

View File

@ -13,15 +13,15 @@ LZLookupTable::LZLookupTable(atInt32 minimumMatch, atInt32 slidingWindow, atInt3
{ {
if(minimumMatch > 0 ) if(minimumMatch > 0 )
m_minimumMatch = minimumMatch; m_minimumMatch = minimumMatch;
else else
m_minimumMatch = 3; m_minimumMatch = 3;
if(slidingWindow > 0) if(slidingWindow > 0)
m_slidingWindow = slidingWindow; m_slidingWindow = slidingWindow;
else else
m_slidingWindow = 4096; m_slidingWindow = 4096;
if(lookAheadWindow > 0) if(lookAheadWindow > 0)
m_lookAheadWindow = lookAheadWindow; m_lookAheadWindow = lookAheadWindow;
else else
m_lookAheadWindow = 18; m_lookAheadWindow = 18;
m_buffer.reserve(m_minimumMatch); m_buffer.reserve(m_minimumMatch);
} }
@ -33,80 +33,80 @@ void LZLookupTable::setLookAheadWindow(atInt32 lookAheadWindow)
{ {
if(lookAheadWindow > 0) if(lookAheadWindow > 0)
m_lookAheadWindow = lookAheadWindow; m_lookAheadWindow = lookAheadWindow;
else else
m_lookAheadWindow = 18; m_lookAheadWindow = 18;
} }
LZLengthOffset LZLookupTable::search(atUint8* curPos, const atUint8* dataBegin, const atUint8* dataEnd) LZLengthOffset LZLookupTable::search(atUint8* curPos, const atUint8* dataBegin, const atUint8* dataEnd)
{ {
LZLengthOffset loPair = {0,0}; LZLengthOffset loPair = {0,0};
//Returns negative 1 for search failures since the current position is passed the size to be compressed //Returns negative 1 for search failures since the current position is passed the size to be compressed
if(curPos >=dataEnd) if(curPos >=dataEnd)
{ {
loPair.length=-1; loPair.length=-1;
return loPair; return loPair;
} }
std::copy(curPos, curPos + m_minimumMatch, m_buffer.begin()); std::copy(curPos, curPos + m_minimumMatch, m_buffer.begin());
int32_t currentOffset = static_cast<atInt32>(curPos - dataBegin); int32_t currentOffset = static_cast<atInt32>(curPos - dataBegin);
//Find code //Find code
if(currentOffset > 0 && (dataEnd - curPos) >= m_minimumMatch) if(currentOffset > 0 && (dataEnd - curPos) >= m_minimumMatch)
{ {
auto elements = table.equal_range(m_buffer); auto elements = table.equal_range(m_buffer);
elements.second--; elements.second--;
elements.first--; elements.first--;
//Iterate over keys in reverse order. C++11 guarantees that the relative order of elements is maintained for the same key //Iterate over keys in reverse order. C++11 guarantees that the relative order of elements is maintained for the same key
for(auto iter = elements.second; iter != elements.first; iter--) for(auto iter = elements.second; iter != elements.first; iter--)
{ {
int32_t matchLength = m_minimumMatch; int32_t matchLength = m_minimumMatch;
int32_t lookAheadBufferLength = ((dataEnd - curPos) < m_lookAheadWindow) ? static_cast<int32_t>(dataEnd - curPos) : m_lookAheadWindow; int32_t lookAheadBufferLength = ((dataEnd - curPos) < m_lookAheadWindow) ? static_cast<int32_t>(dataEnd - curPos) : m_lookAheadWindow;
for(; matchLength < lookAheadBufferLength; ++matchLength) for(; matchLength < lookAheadBufferLength; ++matchLength)
{ {
if(*(dataBegin + iter->second + matchLength) != *(curPos + matchLength)) if(*(dataBegin + iter->second + matchLength) != *(curPos + matchLength))
break; break;
} }
//Store the longest match found so far into length_offset struct. //Store the longest match found so far into length_offset struct.
//When lengths are the same the closer offset to the lookahead buffer wins //When lengths are the same the closer offset to the lookahead buffer wins
if(loPair.length < (atUint32)matchLength) if(loPair.length < (atUint32)matchLength)
{ {
loPair.length = matchLength; loPair.length = matchLength;
loPair.offset = currentOffset - iter->second; loPair.offset = currentOffset - iter->second;
} }
//Found the longest match so break out of loop //Found the longest match so break out of loop
if(loPair.length == (atUint32)m_lookAheadWindow) if(loPair.length == (atUint32)m_lookAheadWindow)
break; break;
} }
} }
//end find code //end find code
//Insert code //Insert code
table.insert(std::make_pair(m_buffer, currentOffset)); table.insert(std::make_pair(m_buffer, currentOffset));
for(atUint32 i = 1; i < loPair.length; i++) for(atUint32 i = 1; i < loPair.length; i++)
{ {
if(dataEnd - (curPos + i) < m_minimumMatch) if(dataEnd - (curPos + i) < m_minimumMatch)
break; break;
std::copy(curPos + i, curPos + m_minimumMatch + i, m_buffer.begin()); std::copy(curPos + i, curPos + m_minimumMatch + i, m_buffer.begin());
table.insert(std::make_pair(m_buffer, currentOffset + i)); table.insert(std::make_pair(m_buffer, currentOffset + i));
} }
//end insert code //end insert code
//Delete code //Delete code
int32_t slidingWindowOffset = std::max(0, currentOffset - m_slidingWindow);//Absolute offset int32_t slidingWindowOffset = std::max(0, currentOffset - m_slidingWindow);//Absolute offset
int32_t tablesize=static_cast<int32_t>(table.size()); int32_t tablesize=static_cast<int32_t>(table.size());
for(int32_t i = 0; i < tablesize - m_slidingWindow; ++i) for(int32_t i = 0; i < tablesize - m_slidingWindow; ++i)
{ {
std::copy(dataBegin + slidingWindowOffset + i, dataBegin + slidingWindowOffset + m_minimumMatch + i, m_buffer.begin()); std::copy(dataBegin + slidingWindowOffset + i, dataBegin + slidingWindowOffset + m_minimumMatch + i, m_buffer.begin());
auto elements = table.equal_range(m_buffer); auto elements = table.equal_range(m_buffer);
for(auto iter = elements.first; iter != elements.second; iter++) for(auto iter = elements.first; iter != elements.second; iter++)
{ {
if(slidingWindowOffset + i == iter->second) if(slidingWindowOffset + i == iter->second)
{ {
table.erase(iter); table.erase(iter);
//There should no occurance of the map with the same value //There should no occurance of the map with the same value
break; break;
} }
} }
} }
//end delete code //end delete code
return loPair; return loPair;
//break lookupTable.cpp:109 if table.size()> 4096 //break lookupTable.cpp:109 if table.size()> 4096
} }