11#include "loggingcategory.h"
22class Q_DECL_HIDDEN KXzFilter::Private
26 : isInitialized(false)
28 memset(&zStream, 0,
sizeof(zStream));
43KXzFilter::~KXzFilter()
48bool KXzFilter::init(
int mode)
51 return init(mode, AUTO, props);
54static void freeFilters(lzma_filter filters[])
56 for (
int i = 0; filters[i].id != LZMA_VLI_UNKNOWN; i++) {
57 free(filters[i].options);
63 if (d->isInitialized) {
69 d->zStream.next_in =
nullptr;
70 d->zStream.avail_in = 0;
73 lzma_filter filters[5];
74 filters[0].id = LZMA_VLI_UNKNOWN;
81 result = lzma_auto_decoder(&d->zStream, 100 << 20, 0);
82 if (result != LZMA_OK) {
83 qCWarning(KArchiveLog) <<
"lzma_auto_decoder returned" << result;
88 filters[0].id = LZMA_FILTER_LZMA1;
89 filters[0].options =
nullptr;
90 filters[1].id = LZMA_VLI_UNKNOWN;
91 filters[1].options =
nullptr;
94 unsigned char props[5];
99 result = lzma_properties_decode(&filters[0],
nullptr, props,
sizeof(props));
100 if (result != LZMA_OK) {
101 qCWarning(KArchiveLog) <<
"lzma_properties_decode returned" << result;
102 freeFilters(filters);
108 filters[0].id = LZMA_FILTER_LZMA2;
109 filters[0].options =
nullptr;
110 filters[1].id = LZMA_VLI_UNKNOWN;
111 filters[1].options =
nullptr;
114 unsigned char props[1];
117 result = lzma_properties_decode(&filters[0],
nullptr, props,
sizeof(props));
118 if (result != LZMA_OK) {
119 qCWarning(KArchiveLog) <<
"lzma_properties_decode returned" << result;
120 freeFilters(filters);
126 filters[0].id = LZMA_FILTER_X86;
127 filters[0].options =
nullptr;
129 unsigned char props[5] = {0x5d, 0x00, 0x00, 0x08, 0x00};
130 filters[1].id = LZMA_FILTER_LZMA1;
131 filters[1].options =
nullptr;
132 result = lzma_properties_decode(&filters[1],
nullptr, props,
sizeof(props));
133 if (result != LZMA_OK) {
134 qCWarning(KArchiveLog) <<
"lzma_properties_decode1 returned" << result;
135 freeFilters(filters);
139 filters[2].id = LZMA_VLI_UNKNOWN;
140 filters[2].options =
nullptr;
154 result = lzma_raw_decoder(&d->zStream, filters);
155 if (result != LZMA_OK) {
156 qCWarning(KArchiveLog) <<
"lzma_raw_decoder returned" << result;
157 freeFilters(filters);
161 freeFilters(filters);
165 result = lzma_easy_encoder(&d->zStream, LZMA_PRESET_DEFAULT, LZMA_CHECK_CRC32);
167 lzma_filter filters[5];
169 lzma_options_lzma lzma_opt;
170 lzma_lzma_preset(&lzma_opt, LZMA_PRESET_DEFAULT);
172 filters[0].id = LZMA_FILTER_LZMA2;
173 filters[0].options = &lzma_opt;
174 filters[1].id = LZMA_VLI_UNKNOWN;
175 filters[1].options =
nullptr;
177 result = lzma_raw_encoder(&d->zStream, filters);
179 if (result != LZMA_OK) {
180 qCWarning(KArchiveLog) <<
"lzma_easy_encoder returned" << result;
188 d->isInitialized =
true;
192int KXzFilter::mode()
const
197bool KXzFilter::terminate()
200 lzma_end(&d->zStream);
205 d->isInitialized =
false;
209void KXzFilter::reset()
217void KXzFilter::setOutBuffer(
char *data, uint maxlen)
219 d->zStream.avail_out = maxlen;
220 d->zStream.next_out = (uint8_t *)data;
223void KXzFilter::setInBuffer(
const char *data,
unsigned int size)
225 d->zStream.avail_in = size;
226 d->zStream.next_in = (uint8_t *)
const_cast<char *
>(data);
229int KXzFilter::inBufferAvailable()
const
231 return d->zStream.avail_in;
234int KXzFilter::outBufferAvailable()
const
236 return d->zStream.avail_out;
239KXzFilter::Result KXzFilter::uncompress()
243 result = lzma_code(&d->zStream, LZMA_RUN);
252 return KFilterBase::Ok;
253 case LZMA_STREAM_END:
254 return KFilterBase::End;
256 return KFilterBase::Error;
260KXzFilter::Result KXzFilter::compress(
bool finish)
263 lzma_ret result = lzma_code(&d->zStream, finish ? LZMA_FINISH : LZMA_RUN);
266 return KFilterBase::Ok;
268 case LZMA_STREAM_END:
270 return KFilterBase::End;
274 return KFilterBase::Error;