-
-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathcompression.cpp
More file actions
185 lines (156 loc) · 5 KB
/
compression.cpp
File metadata and controls
185 lines (156 loc) · 5 KB
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
/*
* Copyright (C) 2020-2021 Matthieu Gautier <mgautier@kymeria.fr>
* Copyright (C) 2020 Emmanuel Engelhart <kelson@kiwix.org>
* Copyright (C) 2020 Veloman Yunkan
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* is provided AS IS, WITHOUT ANY WARRANTY; without even the impliedD
* warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
* NON-INFRINGEMENT. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "compression.h"
#include <zim/tools.h>
#include <stdexcept>
const std::string LZMA_INFO::name = "lzma";
void LZMA_INFO::init_stream_decoder(stream_t* stream, char* raw_data)
{
*stream = LZMA_STREAM_INIT;
auto errcode = lzma_stream_decoder(stream, LZMA_MEMORY_SIZE * 1024 * 1024, 0);
if (errcode != LZMA_OK) {
throw std::runtime_error("Impossible to allocated needed memory to uncompress lzma stream");
}
}
CompStatus LZMA_INFO::stream_run_decode(stream_t* stream, CompStep step) {
return stream_run(stream, step);
}
CompStatus LZMA_INFO::stream_run(stream_t* stream, CompStep step)
{
auto errcode = lzma_code(stream, step==CompStep::STEP?LZMA_RUN:LZMA_FINISH);
switch(errcode) {
case LZMA_BUF_ERROR:
return CompStatus::BUF_ERROR;
case LZMA_STREAM_END:
return CompStatus::STREAM_END;
case LZMA_OK:
return CompStatus::OK;
default: {
throw std::runtime_error(zim::Formatter()
<< "Unexpected lzma status : " << errcode);
}
}
}
void LZMA_INFO::stream_end_decode(stream_t* stream)
{
lzma_end(stream);
}
size_t LZMA_INFO::state_size(const stream_t& stream)
{
return lzma_memusage(&stream);
}
const std::string ZSTD_INFO::name = "zstd";
ZSTD_INFO::stream_t::stream_t()
: next_in(nullptr),
avail_in(0),
next_out(nullptr),
avail_out(0),
total_out(0),
encoder_stream(nullptr),
decoder_stream(nullptr)
{}
ZSTD_INFO::stream_t::~stream_t()
{
if ( encoder_stream )
::ZSTD_freeCStream(encoder_stream);
if ( decoder_stream )
::ZSTD_freeDStream(decoder_stream);
}
void ZSTD_INFO::init_stream_decoder(stream_t* stream, char* raw_data)
{
stream->decoder_stream = ::ZSTD_createDStream();
auto ret = ::ZSTD_initDStream(stream->decoder_stream);
if (::ZSTD_isError(ret)) {
throw std::runtime_error("Failed to initialize Zstd decompression");
}
}
void ZSTD_INFO::init_stream_encoder(stream_t* stream, char* raw_data)
{
stream->encoder_stream = ::ZSTD_createCStream();
auto ret = ::ZSTD_initCStream(stream->encoder_stream, 19);
if (::ZSTD_isError(ret)) {
throw std::runtime_error("Failed to initialize Zstd compression");
}
}
CompStatus ZSTD_INFO::stream_run_encode(stream_t* stream, CompStep step) {
::ZSTD_inBuffer inBuf;
inBuf.src = stream->next_in;
inBuf.size = stream->avail_in;
inBuf.pos = 0;
::ZSTD_outBuffer outBuf;
outBuf.dst = stream->next_out;
outBuf.size = stream->avail_out;
outBuf.pos = 0;
auto ret = step == CompStep::STEP
? ::ZSTD_compressStream(stream->encoder_stream, &outBuf, &inBuf)
: ::ZSTD_endStream(stream->encoder_stream, &outBuf);
stream->next_in += inBuf.pos;
stream->avail_in -= inBuf.pos;
stream->next_out += outBuf.pos;
stream->avail_out -= outBuf.pos;
stream->total_out += outBuf.pos;
if (::ZSTD_isError(ret)) {
throw std::runtime_error(::ZSTD_getErrorName(ret));
}
if ( step == CompStep::STEP ) {
if ( stream->avail_in != 0) {
ASSERT(stream->avail_out, ==, 0u);
return CompStatus::BUF_ERROR;
}
} else if ( ret > 0 ) {
return CompStatus::BUF_ERROR;
}
return CompStatus::OK;
}
CompStatus ZSTD_INFO::stream_run_decode(stream_t* stream, CompStep /*step*/) {
::ZSTD_inBuffer inBuf;
inBuf.src = stream->next_in;
inBuf.size = stream->avail_in;
inBuf.pos = 0;
::ZSTD_outBuffer outBuf;
outBuf.dst = stream->next_out;
outBuf.size = stream->avail_out;
outBuf.pos = 0;
auto ret = ::ZSTD_decompressStream(stream->decoder_stream, &outBuf, &inBuf);
stream->next_in += inBuf.pos;
stream->avail_in -= inBuf.pos;
stream->next_out += outBuf.pos;
stream->avail_out -= outBuf.pos;
stream->total_out += outBuf.pos;
if (::ZSTD_isError(ret))
throw std::runtime_error(::ZSTD_getErrorName(ret));
if (ret == 0)
return CompStatus::STREAM_END;
return CompStatus::BUF_ERROR;
}
void ZSTD_INFO::stream_end_decode(stream_t* stream)
{
}
void ZSTD_INFO::stream_end_encode(stream_t* stream)
{
}
size_t ZSTD_INFO::state_size(const stream_t& stream) {
if (stream.decoder_stream) {
return ZSTD_sizeof_CStream(stream.encoder_stream);
} else {
return ZSTD_sizeof_DStream(stream.decoder_stream);
}
}