Skip to content

Commit

Permalink
Fixed bug where Z_BUF_ERROR error was not handled correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
nickgammon committed Jun 6, 2011
1 parent 00fd050 commit bf9c23b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
41 changes: 33 additions & 8 deletions doc.cpp
Expand Up @@ -1343,7 +1343,7 @@ CString str = strText;

void CMUSHclientDoc::ReceiveMsg()
{
char buff [1000];
char buff [1000]; // must be less than COMPRESS_BUFFER_LENGTH or it won't fit
int count = m_pSocket->Receive (buff, sizeof (buff) - 1);

Frame.CheckTimerFallback (); // see if time is up for timers to fire
Expand Down Expand Up @@ -1470,7 +1470,30 @@ int count = m_pSocket->Receive (buff, sizeof (buff) - 1);
}

// decompress it
int iCompressResult = inflate (&m_zCompress, Z_SYNC_FLUSH);
int iCompressResult;
do {
iCompressResult = inflate (&m_zCompress, Z_SYNC_FLUSH);

// buffer too small? (highly compressed text, huh?)
// make larger, try again - version 4.74
// See: http://www.gammon.com.au/forum/?id=11160
if (iCompressResult == Z_BUF_ERROR)
{
m_nCompressionOutputBufferSize += COMPRESS_BUFFER_LENGTH;
m_zCompress.avail_out += COMPRESS_BUFFER_LENGTH;
m_CompressOutput = (Bytef *) realloc (m_CompressOutput, m_nCompressionOutputBufferSize);

if (m_CompressOutput == NULL)
{
OnConnectionDisconnect (); // close the world
free (m_CompressInput); // may as well get rid of compression input as well
m_CompressInput = NULL;
TMessageBox ("Insufficient memory to decompress MCCP text.", MB_ICONEXCLAMATION);
return;
} // end of cannot get more memory
} // end of Z_BUF_ERROR

} while (iCompressResult == Z_BUF_ERROR);

if (App.m_iCounterFrequency)
{
Expand All @@ -1481,18 +1504,20 @@ int count = m_pSocket->Receive (buff, sizeof (buff) - 1);
// error?
if (iCompressResult < 0)
{
char * pMsg = m_zCompress.msg; // closing may clear the message

OnConnectionDisconnect (); // close the world
if (m_zCompress.msg)
if (pMsg)
UMessageBox (TFormat ("Could not decompress text from MUD: %s",
(LPCTSTR) m_zCompress.msg), MB_ICONEXCLAMATION);
(LPCTSTR) pMsg), MB_ICONEXCLAMATION);
else
UMessageBox (TFormat ("Could not decompress text from MUD: %i",
iCompressResult), MB_ICONEXCLAMATION);
return;
}

// work out how much we got, and display it
int iLength = COMPRESS_BUFFER_LENGTH - m_zCompress.avail_out;
int iLength = m_nCompressionOutputBufferSize - m_zCompress.avail_out;

// stats - count uncompressed bytes
m_nTotalUncompressed += iLength;
Expand All @@ -1507,7 +1532,7 @@ int count = m_pSocket->Receive (buff, sizeof (buff) - 1);

DisplayMsg ((LPCTSTR) m_CompressOutput, iLength, 0); // send uncompressed data to screen
m_zCompress.next_out = m_CompressOutput; // reset for more output
m_zCompress.avail_out = COMPRESS_BUFFER_LENGTH;
m_zCompress.avail_out = m_nCompressionOutputBufferSize;
}

// if end of stream, turn decompression off
Expand Down Expand Up @@ -1971,7 +1996,7 @@ CString strLine (lpszText, size);
m_zCompress.next_in = m_CompressInput;
m_zCompress.avail_in = size;
m_zCompress.next_out = m_CompressOutput;
m_zCompress.avail_out = COMPRESS_BUFFER_LENGTH;
m_zCompress.avail_out = m_nCompressionOutputBufferSize;
m_nTotalCompressed += size;
return; // done with this loop, now it needs to be decompressed
}
Expand All @@ -1993,7 +2018,7 @@ CString strLine (lpszText, size);
m_zCompress.next_in = m_CompressInput;
m_zCompress.avail_in = size;
m_zCompress.next_out = m_CompressOutput;
m_zCompress.avail_out = COMPRESS_BUFFER_LENGTH;
m_zCompress.avail_out = m_nCompressionOutputBufferSize;
m_nTotalCompressed += size;
return; // done with this loop, now it needs to be decompressed
}
Expand Down
1 change: 1 addition & 0 deletions doc.h
Expand Up @@ -1016,6 +1016,7 @@ class CMUSHclientDoc : public CDocument
__int64 m_nTotalUncompressed;
__int64 m_nTotalCompressed;
LONGLONG m_iCompressionTimeTaken; // time taken to decompress
long m_nCompressionOutputBufferSize; // size of decompression output buffer


int m_iMCCP_type; // MCCP protocol type in use: 0 = none, 1 = v1, 2 = v2
Expand Down
1 change: 1 addition & 0 deletions doc_construct.cpp
Expand Up @@ -222,6 +222,7 @@ int i;
m_nTotalUncompressed = 0;
m_nTotalCompressed = 0;
m_iCompressionTimeTaken = 0;
m_nCompressionOutputBufferSize = COMPRESS_BUFFER_LENGTH; // initial value

// we will defer initialising zlib until we really have to

Expand Down
2 changes: 1 addition & 1 deletion telnet_phases.cpp
Expand Up @@ -248,7 +248,7 @@ void CMUSHclientDoc::Phase_WILL (const unsigned char c)
if (m_bCompressInitOK && !m_bDisableCompression)
{
if (!m_CompressOutput)
m_CompressOutput = (Bytef *) malloc (COMPRESS_BUFFER_LENGTH);
m_CompressOutput = (Bytef *) malloc (m_nCompressionOutputBufferSize);
if (!m_CompressInput)
m_CompressInput = (Bytef *) malloc (COMPRESS_BUFFER_LENGTH);

Expand Down

0 comments on commit bf9c23b

Please sign in to comment.