Factor out reusable part of wxLZMAInputStream into wxLZMAData

No changes yet, this is a pure refactoring in preparation for adding
wxLZMAOutputStream.
This commit is contained in:
Vadim Zeitlin
2018-03-30 00:11:31 +02:00
parent af7e2901fe
commit e66ade1b84
2 changed files with 61 additions and 35 deletions

View File

@@ -17,18 +17,48 @@
#include "wx/stream.h" #include "wx/stream.h"
#include "wx/versioninfo.h" #include "wx/versioninfo.h"
namespace wxPrivate
{
// Private wrapper for lzma_stream struct.
struct wxLZMAStream; struct wxLZMAStream;
// Common part of input and output LZMA streams: this is just an implementation
// detail and is not part of the public API.
class WXDLLIMPEXP_BASE wxLZMAData
{
protected:
wxLZMAData();
~wxLZMAData();
wxLZMAStream* m_stream;
wxUint8* m_streamBuf;
wxFileOffset m_pos;
wxDECLARE_NO_COPY_CLASS(wxLZMAData);
};
} // namespace wxPrivate
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Filter for decompressing data compressed using LZMA // Filter for decompressing data compressed using LZMA
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
class WXDLLIMPEXP_BASE wxLZMAInputStream : public wxFilterInputStream class WXDLLIMPEXP_BASE wxLZMAInputStream : public wxFilterInputStream,
private wxPrivate::wxLZMAData
{ {
public: public:
explicit wxLZMAInputStream(wxInputStream& stream); explicit wxLZMAInputStream(wxInputStream& stream)
explicit wxLZMAInputStream(wxInputStream* stream); : wxFilterInputStream(stream)
virtual ~wxLZMAInputStream(); {
Init();
}
explicit wxLZMAInputStream(wxInputStream* stream)
: wxFilterInputStream(stream)
{
Init();
}
char Peek() wxOVERRIDE { return wxInputStream::Peek(); } char Peek() wxOVERRIDE { return wxInputStream::Peek(); }
wxFileOffset GetLength() const wxOVERRIDE { return wxInputStream::GetLength(); } wxFileOffset GetLength() const wxOVERRIDE { return wxInputStream::GetLength(); }
@@ -39,12 +69,6 @@ protected:
private: private:
void Init(); void Init();
wxLZMAStream* m_stream;
wxUint8* m_inbuf;
wxFileOffset m_pos;
wxDECLARE_NO_COPY_CLASS(wxLZMAInputStream);
}; };
WXDLLIMPEXP_BASE wxVersionInfo wxGetLibLZMAVersionInfo(); WXDLLIMPEXP_BASE wxVersionInfo wxGetLibLZMAVersionInfo();

View File

@@ -33,11 +33,14 @@
#include <lzma.h> #include <lzma.h>
namespace wxPrivate
{
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Constants // Constants
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
static const size_t wxLZMA_BUF_SIZE = 4096; const size_t wxLZMA_BUF_SIZE = 4096;
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Private helpers // Private helpers
@@ -58,6 +61,10 @@ struct wxLZMAStream : lzma_stream
} }
}; };
} // namespace wxPrivate
using namespace wxPrivate;
// ============================================================================ // ============================================================================
// implementation // implementation
// ============================================================================ // ============================================================================
@@ -80,28 +87,29 @@ wxVersionInfo wxGetLibLZMAVersionInfo()
); );
} }
// ----------------------------------------------------------------------------
// wxLZMAData: common helpers for compression and decompression
// ----------------------------------------------------------------------------
wxLZMAData::wxLZMAData()
{
m_stream = new wxLZMAStream;
m_streamBuf = new wxUint8[wxLZMA_BUF_SIZE];
m_pos = 0;
}
wxLZMAData::~wxLZMAData()
{
delete [] m_streamBuf;
delete m_stream;
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// wxLZMAInputStream: decompression // wxLZMAInputStream: decompression
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
wxLZMAInputStream::wxLZMAInputStream(wxInputStream& stream)
: wxFilterInputStream(stream)
{
Init();
}
wxLZMAInputStream::wxLZMAInputStream(wxInputStream* stream)
: wxFilterInputStream(stream)
{
Init();
}
void wxLZMAInputStream::Init() void wxLZMAInputStream::Init()
{ {
m_stream = new wxLZMAStream;
m_inbuf = new wxUint8[wxLZMA_BUF_SIZE];
m_pos = 0;
// We don't specify any memory usage limit nor any flags, not even // We don't specify any memory usage limit nor any flags, not even
// LZMA_CONCATENATED recommended by liblzma documentation, because we don't // LZMA_CONCATENATED recommended by liblzma documentation, because we don't
// foresee the need to support concatenated compressed files for now. // foresee the need to support concatenated compressed files for now.
@@ -126,12 +134,6 @@ void wxLZMAInputStream::Init()
m_lasterror = wxSTREAM_READ_ERROR; m_lasterror = wxSTREAM_READ_ERROR;
} }
wxLZMAInputStream::~wxLZMAInputStream()
{
delete [] m_inbuf;
delete m_stream;
}
size_t wxLZMAInputStream::OnSysRead(void* outbuf, size_t size) size_t wxLZMAInputStream::OnSysRead(void* outbuf, size_t size)
{ {
m_stream->next_out = static_cast<uint8_t*>(outbuf); m_stream->next_out = static_cast<uint8_t*>(outbuf);
@@ -145,8 +147,8 @@ size_t wxLZMAInputStream::OnSysRead(void* outbuf, size_t size)
// Get more input data if needed. // Get more input data if needed.
if ( !m_stream->avail_in ) if ( !m_stream->avail_in )
{ {
m_parent_i_stream->Read(m_inbuf, wxLZMA_BUF_SIZE); m_parent_i_stream->Read(m_streamBuf, wxLZMA_BUF_SIZE);
m_stream->next_in = m_inbuf; m_stream->next_in = m_streamBuf;
m_stream->avail_in = m_parent_i_stream->LastRead(); m_stream->avail_in = m_parent_i_stream->LastRead();
if ( !m_stream->avail_in ) if ( !m_stream->avail_in )