File: Otherstreams.cpp

package info (click to toggle)
4pane 5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,252 kB
  • sloc: cpp: 37,352; sh: 4,109; ansic: 3,620; makefile: 157; xml: 27
file content (311 lines) | stat: -rw-r--r-- 9,482 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/////////////////////////////////////////////////////////////////////////////
// Name:        Otherstreams.cpp
// Purpose:     xz and lmza1 archive streams
// Part of:     4Pane
// Author:      David Hart
// Copyright:   (c) 2016 David Hart
// Licence:     GPL v3
/////////////////////////////////////////////////////////////////////////////

#ifndef NO_LZMA_ARCHIVE_STREAMS

#include "wx/wxprec.h"

#include "wx/wx.h"
#include "wx/utils.h"
#include "wx/intl.h"
#include "wx/log.h"

#if wxUSE_STREAMS

#include "Externs.h"
#include "MyFrame.h"
#include "Otherstreams.h"

XzInputStream::XzInputStream(wxInputStream& Stream, bool use_lzma1 /*= false*/) :  wxFilterInputStream(Stream), m_nBufferPos(0)
{
wxCHECK_RET(LIBLZMA_LOADED, wxT("We shouldn't be here if liblzma isn't loaded!"));

lzma_stream_decoder = (dl_lzma_stream_decoder)(wxGetApp().GetLiblzma()->GetSymbol(wxT("lzma_stream_decoder")));
if (!lzma_stream_decoder) { wxLogWarning(wxT("Failed to load symbol 'lzma_stream_decoder'")); return; }
lzma_alone_decoder = (dl_lzma_alone_decoder)(wxGetApp().GetLiblzma()->GetSymbol(wxT("lzma_alone_decoder")));
if (!lzma_alone_decoder) { wxLogWarning(wxT("Failed to load symbol 'lzma_alone_decoder'")); return; }
lzma_code = (dl_lzma_code)(wxGetApp().GetLiblzma()->GetSymbol(wxT("lzma_code")));
if (!lzma_code) { wxLogWarning(wxT("Failed to load symbol 'lzma_code'")); return; }
lzma_end = (dl_lzma_end)(wxGetApp().GetLiblzma()->GetSymbol(wxT("lzma_end")));
if (!lzma_end) { wxLogWarning(wxT("Failed to load symbol 'lzma_end'")); return; }

m_lzStream = (void*) new lzma_stream;
lzma_stream* lzstr = (lzma_stream*)m_lzStream;

memset(lzstr, 0, sizeof(lzma_stream)); // This is the recommended way of initialising a dynamically allocated lzma_stream

/* initialize xz (or lzma1) decoder */
lzma_ret ret_xz;
if (!use_lzma1)
  ret_xz = lzma_stream_decoder(lzstr, UINT64_MAX, LZMA_TELL_UNSUPPORTED_CHECK /*| LZMA_CONCATENATED*/); // The LZMA_CONCATENATED doesn't seem to be needed
 else
  ret_xz = lzma_alone_decoder(lzstr, UINT64_MAX);

if (ret_xz != LZMA_OK) 
  { delete lzstr;
    wxLogSysError(wxT("Could not initialize %s decompression engine!"), use_lzma1 ? wxString(wxT("lzma1")).c_str():wxString(wxT("xz")).c_str());
  }
}

XzInputStream::~XzInputStream()
{
lzma_stream* lzstr = (lzma_stream*)m_lzStream;
lzma_end(lzstr);

delete lzstr;
}

wxInputStream& XzInputStream::ReadRaw(void* pBuffer, size_t size)
{
  return m_parent_i_stream->Read(pBuffer, size);
}

off_t XzInputStream::TellRawI()
{
  return m_parent_i_stream->TellI();
}

off_t XzInputStream::SeekRawI(off_t pos, wxSeekMode sm)
{
  return 0;
}

size_t XzInputStream::OnSysRead(void* buffer, size_t bufsize)
{
// This is a clone of wxZlibInputStream::OnSysRead, adjusted for xz

lzma_stream* lzstr = (lzma_stream*)m_lzStream;

if (!lzstr)
  m_lasterror = wxSTREAM_READ_ERROR;
if (!IsOk() || !bufsize)
  return 0;

lzma_ret ret_xz = LZMA_OK;
lzstr->next_out = (uint8_t*)buffer;
lzstr->avail_out = bufsize;

while (ret_xz == LZMA_OK && lzstr->avail_out > 0) 
  { if (lzstr->avail_in == 0 && m_parent_i_stream->IsOk()) 
      { m_parent_i_stream->Read(m_pBuffer, IN_BUF_MAX);
        lzstr->next_in = m_pBuffer;
        lzstr->avail_in = m_parent_i_stream->LastRead();
      }
    ret_xz = lzma_code(lzstr, LZMA_RUN);
  }

switch (ret_xz) 
  { case LZMA_OK:   break;

    case LZMA_FINISH:
      if (lzstr->avail_out) {
        // 'Inflate' comment was: "Unread any data taken from past the end of the deflate stream, so that
        // any additional data can be read from the underlying stream (the crc in a gzip for example)"
        // I don't know if this is still relevant in xz, but it doesn't seem to do any harm; perhaps because it's not called
        if (lzstr->avail_in) {
          m_parent_i_stream->Reset();
          m_parent_i_stream->Ungetch(lzstr->next_in, lzstr->avail_in);
          lzstr->avail_in = 0;
        }
        m_lasterror = wxSTREAM_EOF;
      }
      break;

    default:
      wxLogError(_("Decompressing a lzma stream failed"));
      m_lasterror = wxSTREAM_READ_ERROR;
  }

bufsize -= lzstr->avail_out;
m_nBufferPos += bufsize;
return bufsize;
}


XzOutputStream::XzOutputStream(wxOutputStream& Stream, bool use_lzma1 /*= false*/) :  wxFilterOutputStream(Stream)
{
wxCHECK_RET(LIBLZMA_LOADED, wxT("We shouldn't be here if liblzma isn't loaded!"));

lzma_easy_encoder = (dl_lzma_easy_encoder)(wxGetApp().GetLiblzma()->GetSymbol(wxT("lzma_easy_encoder")));
if (!lzma_easy_encoder) { wxLogWarning(wxT("Failed to load symbol 'lzma_easy_encoder'")); return; }
lzma_alone_encoder = (dl_lzma_alone_encoder)(wxGetApp().GetLiblzma()->GetSymbol(wxT("lzma_alone_encoder")));
if (!lzma_alone_encoder) { wxLogWarning(wxT("Failed to load symbol 'lzma_alone_encoder'")); return; }
lzma_lzma_preset = (dl_lzma_lzma_preset)(wxGetApp().GetLiblzma()->GetSymbol(wxT("lzma_lzma_preset")));
if (!lzma_lzma_preset) { wxLogWarning(wxT("Failed to load symbol 'lzma_lzma_preset'")); return; }
lzma_code = (dl_lzma_code)(wxGetApp().GetLiblzma()->GetSymbol(wxT("lzma_code")));
if (!lzma_code) { wxLogWarning(wxT("Failed to load symbol 'lzma_code'")); return; }
lzma_end = (dl_lzma_end)(wxGetApp().GetLiblzma()->GetSymbol(wxT("lzma_end")));
if (!lzma_end) { wxLogWarning(wxT("Failed to load symbol 'lzma_end'")); return; }

m_lzStream = (void*) new lzma_stream;
lzma_stream* lzstr = (lzma_stream*)m_lzStream;

memset(lzstr, 0, sizeof(lzma_stream)); // This is the recommended way of initialising a dynamically allocated lzma_stream

lzma_ret ret_xz;
if (!use_lzma1)
  ret_xz = lzma_easy_encoder(lzstr, 6, LZMA_CHECK_CRC64);
 else
  { lzma_options_lzma options;
    lzma_lzma_preset(&options, 7);
    ret_xz = lzma_alone_encoder(lzstr, &options);
  }

if (ret_xz != LZMA_OK) 
  { delete lzstr;
    wxLogSysError(wxT("Could not initialize %s compression engine!"), use_lzma1 ? wxString(wxT("lzma1")).c_str():wxString(wxT("xz")).c_str());
    
  }
}

XzOutputStream::~XzOutputStream()
{
lzma_stream* lzstr = (lzma_stream*)m_lzStream;
lzma_end(lzstr);

delete lzstr;
}

wxOutputStream& XzOutputStream::WriteRaw(void* pBuffer, size_t size)
{
  return m_parent_o_stream->Write(pBuffer, size);
}

off_t XzOutputStream::TellRawO()
{
  return m_parent_o_stream->TellO();
}

off_t XzOutputStream::SeekRawO(off_t pos, wxSeekMode sm)
{
  return 0;
}

size_t XzOutputStream::OnSysWrite(const void* buffer, size_t bufsize)
{
// This is a clone of the xz compression example, xz_pipe_comp.c, adjusted for a wxOutputStream

lzma_stream* lzstr = (lzma_stream*)m_lzStream;

if (!lzstr)
  m_lasterror = wxSTREAM_WRITE_ERROR;
if (!IsOk() || !bufsize)
  return 0;

lzma_ret ret_xz = LZMA_OK;
lzstr->next_in = (uint8_t*)buffer;
lzstr->avail_in = bufsize;

do
  {
    lzstr->next_out = (uint8_t*)m_pBuffer;
    lzstr->avail_out = OUT_BUF_MAX;

    ret_xz = lzma_code(lzstr, LZMA_RUN);

    if ((ret_xz != LZMA_OK) && (ret_xz != LZMA_STREAM_END))
      { m_lasterror = wxSTREAM_WRITE_ERROR;
        wxLogError(_("xz compression failure"));
        return false;
      }

    size_t out_len = OUT_BUF_MAX - lzstr->avail_out;
    m_parent_o_stream->Write(m_pBuffer, out_len);
    if (m_parent_o_stream->LastWrite() != out_len) 
      { m_lasterror = wxSTREAM_WRITE_ERROR;
        wxLogDebug(wxT("XzOutputStream: Error writing to underlying stream"));
        break;
      }
  }
   while (lzstr->avail_out == 0);

bufsize -= lzstr->avail_in;
m_nBufferPos += bufsize;
return bufsize;
}


bool XzOutputStream::Close() // Flushes any remaining compressed data
{
lzma_stream* lzstr = (lzma_stream*)m_lzStream;

if (!lzstr)
  m_lasterror = wxSTREAM_WRITE_ERROR;
if (!IsOk())
  return 0;

lzma_ret ret_xz = LZMA_OK;
lzstr->avail_in = 0;

do
  {
    lzstr->next_out = (uint8_t*)m_pBuffer;
    lzstr->avail_out = OUT_BUF_MAX;

    ret_xz = lzma_code(lzstr, LZMA_FINISH);

    if ((ret_xz != LZMA_OK) && (ret_xz != LZMA_STREAM_END))
      { m_lasterror = wxSTREAM_WRITE_ERROR;
        wxLogError(_("xz compression failure"));
        return false;
      }

    size_t out_len = OUT_BUF_MAX - lzstr->avail_out;
    m_parent_o_stream->Write(m_pBuffer, out_len);
    if (m_parent_o_stream->LastWrite() != out_len) 
      { m_lasterror = wxSTREAM_WRITE_ERROR;
        wxLogDebug(wxT("XzOutputStream: Error writing to underlying stream"));
        break;
      }
  }
   while (lzstr->avail_out == 0);

return wxFilterOutputStream::Close() && IsOk();
}

//-----------------------------------------------------------------------------------------


IMPLEMENT_DYNAMIC_CLASS(XzClassFactory, wxFilterClassFactory)

static XzClassFactory g_XzClassFactory;

XzClassFactory::XzClassFactory()
{
    if (this == &g_XzClassFactory)
        PushFront();
}

const wxChar * const *
XzClassFactory::GetProtocols(wxStreamProtocolType type) const
{
    static const wxChar *protos[] =     
        { wxT("xz"), NULL };
    static const wxChar *mimes[] = 
        { wxT("a\application/x-xz"), 
          wxT("application/x-xz-compressed-tar"), 
          NULL };
    static const wxChar *encs[] = 
        { wxT("xz"), NULL };
    static const wxChar *exts[] =    
        { wxT(".xz"), NULL };
    static const wxChar *empty[] =
        { NULL };

    switch (type) {
        case wxSTREAM_PROTOCOL: return protos;
        case wxSTREAM_MIMETYPE: return mimes;
        case wxSTREAM_ENCODING: return encs;
        case wxSTREAM_FILEEXT:  return exts;
        default:                return empty;
    }
}

#endif  // wxUSE_STREAMS
#endif  // ndef NO_LZMA_ARCHIVE_STREAMS