Skip to content

Commit

Permalink
speed up DEFLATE decompression
Browse files Browse the repository at this point in the history
  • Loading branch information
weidai11 committed Apr 8, 2004
1 parent c2f6c37 commit 391a032
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
33 changes: 22 additions & 11 deletions zinflate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,34 +223,46 @@ void Inflator::IsolatedInitialize(const NameValuePairs &parameters)
m_reader.SkipBits(m_reader.BitsBuffered());
}

inline void Inflator::OutputByte(byte b)
void Inflator::OutputByte(byte b)
{
m_window[m_current++] = b;
if (m_current == m_window.size())
{
ProcessDecompressedData(m_window + m_lastFlush, m_window.size() - m_lastFlush);
m_lastFlush = 0;
m_current = 0;
m_wrappedAround = true;
}
if (m_maxDistance < m_window.size())
m_maxDistance++;
}

void Inflator::OutputString(const byte *string, unsigned int length)
{
while (length--)
OutputByte(*string++);
while (length)
{
unsigned int len = STDMIN(length, m_window.size() - m_current);
memcpy(m_window + m_current, string, len);
m_current += len;
if (m_current == m_window.size())
{
ProcessDecompressedData(m_window + m_lastFlush, m_window.size() - m_lastFlush);
m_lastFlush = 0;
m_current = 0;
m_wrappedAround = true;
}
string += len;
length -= len;
}
}

void Inflator::OutputPast(unsigned int length, unsigned int distance)
{
if (distance > m_maxDistance)
throw BadBlockErr();
unsigned int start;
if (m_current > distance)
if (distance <= m_current)
start = m_current - distance;
else
else if (m_wrappedAround && distance <= m_window.size())
start = m_current + m_window.size() - distance;
else
throw BadBlockErr();

if (start + length > m_window.size())
{
Expand All @@ -268,7 +280,6 @@ void Inflator::OutputPast(unsigned int length, unsigned int distance)
{
memcpy(m_window + m_current, m_window + start, length);
m_current += length;
m_maxDistance = STDMIN((unsigned int)m_window.size(), m_maxDistance + length);
}
}

Expand Down Expand Up @@ -311,7 +322,7 @@ void Inflator::ProcessInput(bool flush)
return;
ProcessPrestreamHeader();
m_state = WAIT_HEADER;
m_maxDistance = 0;
m_wrappedAround = false;
m_current = 0;
m_lastFlush = 0;
m_window.New(1 << GetLog2WindowSize());
Expand Down
4 changes: 2 additions & 2 deletions zinflate.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class Inflator : public AutoSignaling<Filter>

enum State {PRE_STREAM, WAIT_HEADER, DECODING_BODY, POST_STREAM, AFTER_END};
State m_state;
bool m_repeat, m_eof;
bool m_repeat, m_eof, m_wrappedAround;
byte m_blockType;
word16 m_storedLen;
enum NextDecode {LITERAL, LENGTH_BITS, DISTANCE, DISTANCE_BITS};
Expand All @@ -141,7 +141,7 @@ class Inflator : public AutoSignaling<Filter>
HuffmanDecoder m_dynamicLiteralDecoder, m_dynamicDistanceDecoder;
LowFirstBitReader m_reader;
SecByteBlock m_window;
unsigned int m_maxDistance, m_current, m_lastFlush;
unsigned int m_current, m_lastFlush;
};

NAMESPACE_END
Expand Down

0 comments on commit 391a032

Please sign in to comment.