extlzham 0.0.1.PROTOTYPE
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.md +27 -0
- data/README.md +21 -0
- data/Rakefile +143 -0
- data/contrib/lzham/LICENSE +22 -0
- data/contrib/lzham/README.md +209 -0
- data/contrib/lzham/include/lzham.h +781 -0
- data/contrib/lzham/lzhamcomp/lzham_comp.h +38 -0
- data/contrib/lzham/lzhamcomp/lzham_lzbase.cpp +244 -0
- data/contrib/lzham/lzhamcomp/lzham_lzbase.h +45 -0
- data/contrib/lzham/lzhamcomp/lzham_lzcomp.cpp +608 -0
- data/contrib/lzham/lzhamcomp/lzham_lzcomp_internal.cpp +1966 -0
- data/contrib/lzham/lzhamcomp/lzham_lzcomp_internal.h +472 -0
- data/contrib/lzham/lzhamcomp/lzham_lzcomp_state.cpp +1413 -0
- data/contrib/lzham/lzhamcomp/lzham_match_accel.cpp +562 -0
- data/contrib/lzham/lzhamcomp/lzham_match_accel.h +146 -0
- data/contrib/lzham/lzhamcomp/lzham_null_threading.h +97 -0
- data/contrib/lzham/lzhamcomp/lzham_pthreads_threading.cpp +229 -0
- data/contrib/lzham/lzhamcomp/lzham_pthreads_threading.h +520 -0
- data/contrib/lzham/lzhamcomp/lzham_threading.h +12 -0
- data/contrib/lzham/lzhamcomp/lzham_win32_threading.cpp +220 -0
- data/contrib/lzham/lzhamcomp/lzham_win32_threading.h +368 -0
- data/contrib/lzham/lzhamdecomp/lzham_assert.cpp +66 -0
- data/contrib/lzham/lzhamdecomp/lzham_assert.h +40 -0
- data/contrib/lzham/lzhamdecomp/lzham_checksum.cpp +73 -0
- data/contrib/lzham/lzhamdecomp/lzham_checksum.h +13 -0
- data/contrib/lzham/lzhamdecomp/lzham_config.h +23 -0
- data/contrib/lzham/lzhamdecomp/lzham_core.h +264 -0
- data/contrib/lzham/lzhamdecomp/lzham_decomp.h +37 -0
- data/contrib/lzham/lzhamdecomp/lzham_helpers.h +54 -0
- data/contrib/lzham/lzhamdecomp/lzham_huffman_codes.cpp +262 -0
- data/contrib/lzham/lzhamdecomp/lzham_huffman_codes.h +14 -0
- data/contrib/lzham/lzhamdecomp/lzham_lzdecomp.cpp +1527 -0
- data/contrib/lzham/lzhamdecomp/lzham_lzdecompbase.cpp +131 -0
- data/contrib/lzham/lzhamdecomp/lzham_lzdecompbase.h +89 -0
- data/contrib/lzham/lzhamdecomp/lzham_math.h +142 -0
- data/contrib/lzham/lzhamdecomp/lzham_mem.cpp +284 -0
- data/contrib/lzham/lzhamdecomp/lzham_mem.h +112 -0
- data/contrib/lzham/lzhamdecomp/lzham_platform.cpp +157 -0
- data/contrib/lzham/lzhamdecomp/lzham_platform.h +284 -0
- data/contrib/lzham/lzhamdecomp/lzham_prefix_coding.cpp +351 -0
- data/contrib/lzham/lzhamdecomp/lzham_prefix_coding.h +146 -0
- data/contrib/lzham/lzhamdecomp/lzham_symbol_codec.cpp +1484 -0
- data/contrib/lzham/lzhamdecomp/lzham_symbol_codec.h +556 -0
- data/contrib/lzham/lzhamdecomp/lzham_timer.cpp +147 -0
- data/contrib/lzham/lzhamdecomp/lzham_timer.h +99 -0
- data/contrib/lzham/lzhamdecomp/lzham_traits.h +141 -0
- data/contrib/lzham/lzhamdecomp/lzham_types.h +97 -0
- data/contrib/lzham/lzhamdecomp/lzham_utils.h +58 -0
- data/contrib/lzham/lzhamdecomp/lzham_vector.cpp +75 -0
- data/contrib/lzham/lzhamdecomp/lzham_vector.h +588 -0
- data/contrib/lzham/lzhamlib/lzham_lib.cpp +179 -0
- data/examples/basic.rb +48 -0
- data/ext/extconf.rb +26 -0
- data/ext/extlzham.c +741 -0
- data/gemstub.rb +22 -0
- data/lib/extlzham/version.rb +5 -0
- data/lib/extlzham.rb +153 -0
- metadata +135 -0
@@ -0,0 +1,608 @@
|
|
1
|
+
// File: lzham_lzcomp.cpp
|
2
|
+
// See Copyright Notice and license at the end of include/lzham.h
|
3
|
+
#include "lzham_core.h"
|
4
|
+
#include "lzham.h"
|
5
|
+
#include "lzham_comp.h"
|
6
|
+
#include "lzham_lzcomp_internal.h"
|
7
|
+
|
8
|
+
using namespace lzham;
|
9
|
+
|
10
|
+
namespace lzham
|
11
|
+
{
|
12
|
+
struct lzham_compress_state
|
13
|
+
{
|
14
|
+
// task_pool requires 8 or 16 alignment
|
15
|
+
task_pool m_tp;
|
16
|
+
lzcompressor m_compressor;
|
17
|
+
|
18
|
+
uint m_dict_size_log2;
|
19
|
+
|
20
|
+
const uint8 *m_pIn_buf;
|
21
|
+
size_t *m_pIn_buf_size;
|
22
|
+
uint8 *m_pOut_buf;
|
23
|
+
size_t *m_pOut_buf_size;
|
24
|
+
|
25
|
+
size_t m_comp_data_ofs;
|
26
|
+
|
27
|
+
bool m_finished_compression;
|
28
|
+
|
29
|
+
lzham_compress_params m_params;
|
30
|
+
|
31
|
+
lzham_compress_status_t m_status;
|
32
|
+
};
|
33
|
+
|
34
|
+
static lzham_compress_status_t create_internal_init_params(lzcompressor::init_params &internal_params, const lzham_compress_params *pParams)
|
35
|
+
{
|
36
|
+
if ((pParams->m_dict_size_log2 < CLZBase::cMinDictSizeLog2) || (pParams->m_dict_size_log2 > CLZBase::cMaxDictSizeLog2))
|
37
|
+
return LZHAM_COMP_STATUS_INVALID_PARAMETER;
|
38
|
+
|
39
|
+
internal_params.m_dict_size_log2 = pParams->m_dict_size_log2;
|
40
|
+
|
41
|
+
if (pParams->m_max_helper_threads < 0)
|
42
|
+
internal_params.m_max_helper_threads = lzham_get_max_helper_threads();
|
43
|
+
else
|
44
|
+
internal_params.m_max_helper_threads = pParams->m_max_helper_threads;
|
45
|
+
internal_params.m_max_helper_threads = LZHAM_MIN(LZHAM_MAX_HELPER_THREADS, internal_params.m_max_helper_threads);
|
46
|
+
|
47
|
+
internal_params.m_lzham_compress_flags = pParams->m_compress_flags;
|
48
|
+
|
49
|
+
if (pParams->m_num_seed_bytes)
|
50
|
+
{
|
51
|
+
if ((!pParams->m_pSeed_bytes) || (pParams->m_num_seed_bytes > (1U << pParams->m_dict_size_log2)))
|
52
|
+
return LZHAM_COMP_STATUS_INVALID_PARAMETER;
|
53
|
+
|
54
|
+
internal_params.m_num_seed_bytes = pParams->m_num_seed_bytes;
|
55
|
+
internal_params.m_pSeed_bytes = pParams->m_pSeed_bytes;
|
56
|
+
}
|
57
|
+
|
58
|
+
switch (pParams->m_level)
|
59
|
+
{
|
60
|
+
case LZHAM_COMP_LEVEL_FASTEST: internal_params.m_compression_level = cCompressionLevelFastest; break;
|
61
|
+
case LZHAM_COMP_LEVEL_FASTER: internal_params.m_compression_level = cCompressionLevelFaster; break;
|
62
|
+
case LZHAM_COMP_LEVEL_DEFAULT: internal_params.m_compression_level = cCompressionLevelDefault; break;
|
63
|
+
case LZHAM_COMP_LEVEL_BETTER: internal_params.m_compression_level = cCompressionLevelBetter; break;
|
64
|
+
case LZHAM_COMP_LEVEL_UBER: internal_params.m_compression_level = cCompressionLevelUber; break;
|
65
|
+
default:
|
66
|
+
return LZHAM_COMP_STATUS_INVALID_PARAMETER;
|
67
|
+
};
|
68
|
+
|
69
|
+
if (pParams->m_table_max_update_interval || pParams->m_table_update_interval_slow_rate)
|
70
|
+
{
|
71
|
+
internal_params.m_table_max_update_interval = pParams->m_table_max_update_interval;
|
72
|
+
internal_params.m_table_update_interval_slow_rate = pParams->m_table_update_interval_slow_rate;
|
73
|
+
}
|
74
|
+
else
|
75
|
+
{
|
76
|
+
uint rate = pParams->m_table_update_rate;
|
77
|
+
if (!rate)
|
78
|
+
rate = LZHAM_DEFAULT_TABLE_UPDATE_RATE;
|
79
|
+
rate = math::clamp<uint>(rate, 1, LZHAM_FASTEST_TABLE_UPDATE_RATE) - 1;
|
80
|
+
internal_params.m_table_max_update_interval = g_table_update_settings[rate].m_max_update_interval;
|
81
|
+
internal_params.m_table_update_interval_slow_rate = g_table_update_settings[rate].m_slow_rate;
|
82
|
+
}
|
83
|
+
|
84
|
+
return LZHAM_COMP_STATUS_SUCCESS;
|
85
|
+
}
|
86
|
+
|
87
|
+
lzham_compress_state_ptr LZHAM_CDECL lzham_lib_compress_init(const lzham_compress_params *pParams)
|
88
|
+
{
|
89
|
+
if ((!pParams) || (pParams->m_struct_size != sizeof(lzham_compress_params)))
|
90
|
+
return NULL;
|
91
|
+
|
92
|
+
if ((pParams->m_dict_size_log2 < CLZBase::cMinDictSizeLog2) || (pParams->m_dict_size_log2 > CLZBase::cMaxDictSizeLog2))
|
93
|
+
return NULL;
|
94
|
+
|
95
|
+
lzcompressor::init_params internal_params;
|
96
|
+
lzham_compress_status_t status = create_internal_init_params(internal_params, pParams);
|
97
|
+
if (status != LZHAM_COMP_STATUS_SUCCESS)
|
98
|
+
return NULL;
|
99
|
+
|
100
|
+
lzham_compress_state *pState = lzham_new<lzham_compress_state>();
|
101
|
+
if (!pState)
|
102
|
+
return NULL;
|
103
|
+
|
104
|
+
pState->m_params = *pParams;
|
105
|
+
|
106
|
+
pState->m_pIn_buf = NULL;
|
107
|
+
pState->m_pIn_buf_size = NULL;
|
108
|
+
pState->m_pOut_buf = NULL;
|
109
|
+
pState->m_pOut_buf_size = NULL;
|
110
|
+
pState->m_status = LZHAM_COMP_STATUS_NOT_FINISHED;
|
111
|
+
pState->m_comp_data_ofs = 0;
|
112
|
+
pState->m_finished_compression = false;
|
113
|
+
|
114
|
+
if (internal_params.m_max_helper_threads)
|
115
|
+
{
|
116
|
+
if (!pState->m_tp.init(internal_params.m_max_helper_threads))
|
117
|
+
{
|
118
|
+
lzham_delete(pState);
|
119
|
+
return NULL;
|
120
|
+
}
|
121
|
+
if (pState->m_tp.get_num_threads() >= internal_params.m_max_helper_threads)
|
122
|
+
{
|
123
|
+
internal_params.m_pTask_pool = &pState->m_tp;
|
124
|
+
}
|
125
|
+
else
|
126
|
+
{
|
127
|
+
internal_params.m_max_helper_threads = 0;
|
128
|
+
}
|
129
|
+
}
|
130
|
+
|
131
|
+
if (!pState->m_compressor.init(internal_params))
|
132
|
+
{
|
133
|
+
lzham_delete(pState);
|
134
|
+
return NULL;
|
135
|
+
}
|
136
|
+
|
137
|
+
return pState;
|
138
|
+
}
|
139
|
+
|
140
|
+
lzham_compress_state_ptr LZHAM_CDECL lzham_lib_compress_reinit(lzham_compress_state_ptr p)
|
141
|
+
{
|
142
|
+
lzham_compress_state *pState = static_cast<lzham_compress_state*>(p);
|
143
|
+
if (pState)
|
144
|
+
{
|
145
|
+
if (!pState->m_compressor.reset())
|
146
|
+
return NULL;
|
147
|
+
|
148
|
+
pState->m_pIn_buf = NULL;
|
149
|
+
pState->m_pIn_buf_size = NULL;
|
150
|
+
pState->m_pOut_buf = NULL;
|
151
|
+
pState->m_pOut_buf_size = NULL;
|
152
|
+
pState->m_status = LZHAM_COMP_STATUS_NOT_FINISHED;
|
153
|
+
pState->m_comp_data_ofs = 0;
|
154
|
+
pState->m_finished_compression = false;
|
155
|
+
}
|
156
|
+
|
157
|
+
return pState;
|
158
|
+
}
|
159
|
+
|
160
|
+
lzham_uint32 LZHAM_CDECL lzham_lib_compress_deinit(lzham_compress_state_ptr p)
|
161
|
+
{
|
162
|
+
lzham_compress_state *pState = static_cast<lzham_compress_state *>(p);
|
163
|
+
if (!pState)
|
164
|
+
return 0;
|
165
|
+
|
166
|
+
uint32 adler32 = pState->m_compressor.get_src_adler32();
|
167
|
+
|
168
|
+
lzham_delete(pState);
|
169
|
+
|
170
|
+
return adler32;
|
171
|
+
}
|
172
|
+
|
173
|
+
lzham_compress_status_t LZHAM_CDECL lzham_lib_compress(
|
174
|
+
lzham_compress_state_ptr p,
|
175
|
+
const lzham_uint8 *pIn_buf, size_t *pIn_buf_size,
|
176
|
+
lzham_uint8 *pOut_buf, size_t *pOut_buf_size,
|
177
|
+
lzham_bool no_more_input_bytes_flag)
|
178
|
+
{
|
179
|
+
return lzham_lib_compress2(p, pIn_buf, pIn_buf_size, pOut_buf, pOut_buf_size, no_more_input_bytes_flag ? LZHAM_FINISH : LZHAM_NO_FLUSH);
|
180
|
+
}
|
181
|
+
|
182
|
+
lzham_compress_status_t LZHAM_CDECL lzham_lib_compress2(
|
183
|
+
lzham_compress_state_ptr p,
|
184
|
+
const lzham_uint8 *pIn_buf, size_t *pIn_buf_size,
|
185
|
+
lzham_uint8 *pOut_buf, size_t *pOut_buf_size,
|
186
|
+
lzham_flush_t flush_type)
|
187
|
+
{
|
188
|
+
lzham_compress_state *pState = static_cast<lzham_compress_state*>(p);
|
189
|
+
|
190
|
+
if ((!pState) || (!pState->m_params.m_dict_size_log2) || (pState->m_status >= LZHAM_COMP_STATUS_FIRST_SUCCESS_OR_FAILURE_CODE) || (!pIn_buf_size) || (!pOut_buf_size))
|
191
|
+
return LZHAM_COMP_STATUS_INVALID_PARAMETER;
|
192
|
+
|
193
|
+
if ((*pIn_buf_size) && (!pIn_buf))
|
194
|
+
return LZHAM_COMP_STATUS_INVALID_PARAMETER;
|
195
|
+
|
196
|
+
if ((!*pOut_buf_size) || (!pOut_buf))
|
197
|
+
return LZHAM_COMP_STATUS_INVALID_PARAMETER;
|
198
|
+
|
199
|
+
byte_vec &comp_data = pState->m_compressor.get_compressed_data();
|
200
|
+
size_t num_bytes_written_to_out_buf = 0;
|
201
|
+
if (pState->m_comp_data_ofs < comp_data.size())
|
202
|
+
{
|
203
|
+
size_t n = LZHAM_MIN(comp_data.size() - pState->m_comp_data_ofs, *pOut_buf_size);
|
204
|
+
|
205
|
+
memcpy(pOut_buf, comp_data.get_ptr() + pState->m_comp_data_ofs, n);
|
206
|
+
|
207
|
+
pState->m_comp_data_ofs += n;
|
208
|
+
|
209
|
+
const bool has_no_more_output = (pState->m_comp_data_ofs >= comp_data.size());
|
210
|
+
if (has_no_more_output)
|
211
|
+
{
|
212
|
+
pOut_buf += n;
|
213
|
+
*pOut_buf_size -= n;
|
214
|
+
num_bytes_written_to_out_buf += n;
|
215
|
+
}
|
216
|
+
else
|
217
|
+
{
|
218
|
+
*pIn_buf_size = 0;
|
219
|
+
*pOut_buf_size = n;
|
220
|
+
pState->m_status = LZHAM_COMP_STATUS_HAS_MORE_OUTPUT;
|
221
|
+
return pState->m_status;
|
222
|
+
}
|
223
|
+
}
|
224
|
+
|
225
|
+
comp_data.try_resize(0);
|
226
|
+
pState->m_comp_data_ofs = 0;
|
227
|
+
|
228
|
+
if (pState->m_finished_compression)
|
229
|
+
{
|
230
|
+
if ((*pIn_buf_size) || (flush_type != LZHAM_FINISH))
|
231
|
+
{
|
232
|
+
pState->m_status = LZHAM_COMP_STATUS_INVALID_PARAMETER;
|
233
|
+
return pState->m_status;
|
234
|
+
}
|
235
|
+
|
236
|
+
*pIn_buf_size = 0;
|
237
|
+
*pOut_buf_size = num_bytes_written_to_out_buf;
|
238
|
+
|
239
|
+
pState->m_status = LZHAM_COMP_STATUS_SUCCESS;
|
240
|
+
return pState->m_status;
|
241
|
+
}
|
242
|
+
|
243
|
+
const size_t cMaxBytesToPutPerIteration = 4*1024*1024;
|
244
|
+
size_t bytes_to_put = LZHAM_MIN(cMaxBytesToPutPerIteration, *pIn_buf_size);
|
245
|
+
const bool consumed_entire_input_buf = (bytes_to_put == *pIn_buf_size);
|
246
|
+
|
247
|
+
if (bytes_to_put)
|
248
|
+
{
|
249
|
+
if (!pState->m_compressor.put_bytes(pIn_buf, (uint)bytes_to_put))
|
250
|
+
{
|
251
|
+
*pIn_buf_size = 0;
|
252
|
+
*pOut_buf_size = num_bytes_written_to_out_buf;
|
253
|
+
pState->m_status = LZHAM_COMP_STATUS_FAILED;
|
254
|
+
return pState->m_status;
|
255
|
+
}
|
256
|
+
}
|
257
|
+
|
258
|
+
if ((consumed_entire_input_buf) && (flush_type != LZHAM_NO_FLUSH))
|
259
|
+
{
|
260
|
+
if ((flush_type == LZHAM_SYNC_FLUSH) || (flush_type == LZHAM_FULL_FLUSH) || (flush_type == LZHAM_TABLE_FLUSH))
|
261
|
+
{
|
262
|
+
if (!pState->m_compressor.flush(flush_type))
|
263
|
+
{
|
264
|
+
*pIn_buf_size = 0;
|
265
|
+
*pOut_buf_size = num_bytes_written_to_out_buf;
|
266
|
+
pState->m_status = LZHAM_COMP_STATUS_FAILED;
|
267
|
+
return pState->m_status;
|
268
|
+
}
|
269
|
+
}
|
270
|
+
else if (!pState->m_finished_compression)
|
271
|
+
{
|
272
|
+
if (!pState->m_compressor.put_bytes(NULL, 0))
|
273
|
+
{
|
274
|
+
*pIn_buf_size = 0;
|
275
|
+
*pOut_buf_size = num_bytes_written_to_out_buf;
|
276
|
+
pState->m_status = LZHAM_COMP_STATUS_FAILED;
|
277
|
+
return pState->m_status;
|
278
|
+
}
|
279
|
+
pState->m_finished_compression = true;
|
280
|
+
}
|
281
|
+
}
|
282
|
+
|
283
|
+
size_t num_comp_bytes_to_output = LZHAM_MIN(comp_data.size() - pState->m_comp_data_ofs, *pOut_buf_size);
|
284
|
+
if (num_comp_bytes_to_output)
|
285
|
+
{
|
286
|
+
memcpy(pOut_buf, comp_data.get_ptr() + pState->m_comp_data_ofs, num_comp_bytes_to_output);
|
287
|
+
|
288
|
+
pState->m_comp_data_ofs += num_comp_bytes_to_output;
|
289
|
+
}
|
290
|
+
|
291
|
+
*pIn_buf_size = bytes_to_put;
|
292
|
+
*pOut_buf_size = num_bytes_written_to_out_buf + num_comp_bytes_to_output;
|
293
|
+
|
294
|
+
const bool has_no_more_output = (pState->m_comp_data_ofs >= comp_data.size());
|
295
|
+
if ((has_no_more_output) && (flush_type == LZHAM_FINISH) && (pState->m_finished_compression))
|
296
|
+
pState->m_status = LZHAM_COMP_STATUS_SUCCESS;
|
297
|
+
else if ((has_no_more_output) && (consumed_entire_input_buf) && (flush_type == LZHAM_NO_FLUSH))
|
298
|
+
pState->m_status = LZHAM_COMP_STATUS_NEEDS_MORE_INPUT;
|
299
|
+
else
|
300
|
+
pState->m_status = has_no_more_output ? LZHAM_COMP_STATUS_NOT_FINISHED : LZHAM_COMP_STATUS_HAS_MORE_OUTPUT;
|
301
|
+
|
302
|
+
return pState->m_status;
|
303
|
+
}
|
304
|
+
|
305
|
+
lzham_compress_status_t LZHAM_CDECL lzham_lib_compress_memory(const lzham_compress_params *pParams, lzham_uint8* pDst_buf, size_t *pDst_len, const lzham_uint8* pSrc_buf, size_t src_len, lzham_uint32 *pAdler32)
|
306
|
+
{
|
307
|
+
if ((!pParams) || (!pDst_len))
|
308
|
+
return LZHAM_COMP_STATUS_INVALID_PARAMETER;
|
309
|
+
|
310
|
+
if (src_len)
|
311
|
+
{
|
312
|
+
if (!pSrc_buf)
|
313
|
+
return LZHAM_COMP_STATUS_INVALID_PARAMETER;
|
314
|
+
}
|
315
|
+
|
316
|
+
if (sizeof(size_t) > sizeof(uint32))
|
317
|
+
{
|
318
|
+
if (src_len > UINT32_MAX)
|
319
|
+
return LZHAM_COMP_STATUS_INVALID_PARAMETER;
|
320
|
+
}
|
321
|
+
|
322
|
+
lzcompressor::init_params internal_params;
|
323
|
+
lzham_compress_status_t status = create_internal_init_params(internal_params, pParams);
|
324
|
+
if (status != LZHAM_COMP_STATUS_SUCCESS)
|
325
|
+
return status;
|
326
|
+
|
327
|
+
task_pool *pTP = NULL;
|
328
|
+
if (internal_params.m_max_helper_threads)
|
329
|
+
{
|
330
|
+
pTP = lzham_new<task_pool>();
|
331
|
+
if (!pTP->init(internal_params.m_max_helper_threads))
|
332
|
+
return LZHAM_COMP_STATUS_FAILED;
|
333
|
+
|
334
|
+
internal_params.m_pTask_pool = pTP;
|
335
|
+
}
|
336
|
+
|
337
|
+
lzcompressor *pCompressor = lzham_new<lzcompressor>();
|
338
|
+
if (!pCompressor)
|
339
|
+
{
|
340
|
+
lzham_delete(pTP);
|
341
|
+
return LZHAM_COMP_STATUS_FAILED;
|
342
|
+
}
|
343
|
+
|
344
|
+
if (!pCompressor->init(internal_params))
|
345
|
+
{
|
346
|
+
lzham_delete(pTP);
|
347
|
+
lzham_delete(pCompressor);
|
348
|
+
return LZHAM_COMP_STATUS_INVALID_PARAMETER;
|
349
|
+
}
|
350
|
+
|
351
|
+
if (src_len)
|
352
|
+
{
|
353
|
+
if (!pCompressor->put_bytes(pSrc_buf, static_cast<uint32>(src_len)))
|
354
|
+
{
|
355
|
+
*pDst_len = 0;
|
356
|
+
lzham_delete(pTP);
|
357
|
+
lzham_delete(pCompressor);
|
358
|
+
return LZHAM_COMP_STATUS_FAILED;
|
359
|
+
}
|
360
|
+
}
|
361
|
+
|
362
|
+
if (!pCompressor->put_bytes(NULL, 0))
|
363
|
+
{
|
364
|
+
*pDst_len = 0;
|
365
|
+
lzham_delete(pTP);
|
366
|
+
lzham_delete(pCompressor);
|
367
|
+
return LZHAM_COMP_STATUS_FAILED;
|
368
|
+
}
|
369
|
+
|
370
|
+
const byte_vec &comp_data = pCompressor->get_compressed_data();
|
371
|
+
|
372
|
+
size_t dst_buf_size = *pDst_len;
|
373
|
+
*pDst_len = comp_data.size();
|
374
|
+
|
375
|
+
if (pAdler32)
|
376
|
+
*pAdler32 = pCompressor->get_src_adler32();
|
377
|
+
|
378
|
+
if (comp_data.size() > dst_buf_size)
|
379
|
+
{
|
380
|
+
lzham_delete(pTP);
|
381
|
+
lzham_delete(pCompressor);
|
382
|
+
return LZHAM_COMP_STATUS_OUTPUT_BUF_TOO_SMALL;
|
383
|
+
}
|
384
|
+
|
385
|
+
memcpy(pDst_buf, comp_data.get_ptr(), comp_data.size());
|
386
|
+
|
387
|
+
lzham_delete(pTP);
|
388
|
+
lzham_delete(pCompressor);
|
389
|
+
return LZHAM_COMP_STATUS_SUCCESS;
|
390
|
+
}
|
391
|
+
|
392
|
+
// ----------------- zlib-style API's
|
393
|
+
|
394
|
+
int lzham_lib_z_deflateInit(lzham_z_streamp pStream, int level)
|
395
|
+
{
|
396
|
+
return lzham_lib_z_deflateInit2(pStream, level, LZHAM_Z_LZHAM, LZHAM_Z_DEFAULT_WINDOW_BITS, 9, LZHAM_Z_DEFAULT_STRATEGY);
|
397
|
+
}
|
398
|
+
|
399
|
+
int lzham_lib_z_deflateInit2(lzham_z_streamp pStream, int level, int method, int window_bits, int mem_level, int strategy)
|
400
|
+
{
|
401
|
+
LZHAM_NOTE_UNUSED(strategy);
|
402
|
+
|
403
|
+
if (!pStream)
|
404
|
+
return LZHAM_Z_STREAM_ERROR;
|
405
|
+
if ((mem_level < 1) || (mem_level > 9))
|
406
|
+
return LZHAM_Z_PARAM_ERROR;
|
407
|
+
if ((method != LZHAM_Z_DEFLATED) && (method != LZHAM_Z_LZHAM))
|
408
|
+
return LZHAM_Z_PARAM_ERROR;
|
409
|
+
|
410
|
+
if (level == LZHAM_Z_DEFAULT_COMPRESSION)
|
411
|
+
level = 9;
|
412
|
+
|
413
|
+
if (method == LZHAM_Z_DEFLATED)
|
414
|
+
{
|
415
|
+
// Force Deflate to LZHAM with default window_bits.
|
416
|
+
method = LZHAM_Z_LZHAM;
|
417
|
+
window_bits = LZHAM_Z_DEFAULT_WINDOW_BITS;
|
418
|
+
}
|
419
|
+
|
420
|
+
#ifdef LZHAM_Z_API_FORCE_WINDOW_BITS
|
421
|
+
window_bits = LZHAM_Z_API_FORCE_WINDOW_BITS;
|
422
|
+
#endif
|
423
|
+
|
424
|
+
int max_window_bits = LZHAM_64BIT_POINTERS ? LZHAM_MAX_DICT_SIZE_LOG2_X64 : LZHAM_MAX_DICT_SIZE_LOG2_X86;
|
425
|
+
if ((labs(window_bits) < LZHAM_MIN_DICT_SIZE_LOG2) || (labs(window_bits) > max_window_bits))
|
426
|
+
return LZHAM_Z_PARAM_ERROR;
|
427
|
+
|
428
|
+
lzham_compress_params comp_params;
|
429
|
+
|
430
|
+
utils::zero_object(comp_params);
|
431
|
+
comp_params.m_struct_size = sizeof(lzham_compress_params);
|
432
|
+
|
433
|
+
comp_params.m_level = LZHAM_COMP_LEVEL_UBER;
|
434
|
+
if (level <= 1)
|
435
|
+
comp_params.m_level = LZHAM_COMP_LEVEL_FASTEST;
|
436
|
+
else if (level <= 3)
|
437
|
+
comp_params.m_level = LZHAM_COMP_LEVEL_FASTER;
|
438
|
+
else if (level <= 5)
|
439
|
+
comp_params.m_level = LZHAM_COMP_LEVEL_DEFAULT;
|
440
|
+
else if (level <= 7)
|
441
|
+
comp_params.m_level = LZHAM_COMP_LEVEL_BETTER;
|
442
|
+
|
443
|
+
if (level == 10)
|
444
|
+
comp_params.m_compress_flags |= LZHAM_COMP_FLAG_EXTREME_PARSING;
|
445
|
+
|
446
|
+
// Use all CPU's. TODO: This is not always the best idea depending on the dictionary size and the # of bytes to compress.
|
447
|
+
comp_params.m_max_helper_threads = -1;
|
448
|
+
|
449
|
+
comp_params.m_dict_size_log2 = static_cast<lzham_uint32>(labs(window_bits));
|
450
|
+
|
451
|
+
if (window_bits > 0)
|
452
|
+
comp_params.m_compress_flags |= LZHAM_COMP_FLAG_WRITE_ZLIB_STREAM;
|
453
|
+
|
454
|
+
pStream->data_type = 0;
|
455
|
+
pStream->adler = LZHAM_Z_ADLER32_INIT;
|
456
|
+
pStream->msg = NULL;
|
457
|
+
pStream->reserved = 0;
|
458
|
+
pStream->total_in = 0;
|
459
|
+
pStream->total_out = 0;
|
460
|
+
|
461
|
+
lzham_compress_state_ptr pComp = lzham_lib_compress_init(&comp_params);
|
462
|
+
if (!pComp)
|
463
|
+
return LZHAM_Z_PARAM_ERROR;
|
464
|
+
|
465
|
+
pStream->state = (struct lzham_z_internal_state *)pComp;
|
466
|
+
|
467
|
+
return LZHAM_Z_OK;
|
468
|
+
}
|
469
|
+
|
470
|
+
int lzham_lib_z_deflateReset(lzham_z_streamp pStream)
|
471
|
+
{
|
472
|
+
if (!pStream)
|
473
|
+
return LZHAM_Z_STREAM_ERROR;
|
474
|
+
|
475
|
+
lzham_compress_state_ptr pComp = (lzham_compress_state_ptr)pStream->state;
|
476
|
+
if (!pComp)
|
477
|
+
return LZHAM_Z_STREAM_ERROR;
|
478
|
+
|
479
|
+
pComp = lzham_lib_compress_reinit(pComp);
|
480
|
+
if (!pComp)
|
481
|
+
return LZHAM_Z_STREAM_ERROR;
|
482
|
+
|
483
|
+
pStream->state = (struct lzham_z_internal_state *)pComp;
|
484
|
+
|
485
|
+
return LZHAM_Z_OK;
|
486
|
+
}
|
487
|
+
|
488
|
+
int lzham_lib_z_deflate(lzham_z_streamp pStream, int flush)
|
489
|
+
{
|
490
|
+
if ((!pStream) || (!pStream->state) || (flush < 0) || (flush > LZHAM_Z_FINISH) || (!pStream->next_out))
|
491
|
+
return LZHAM_Z_STREAM_ERROR;
|
492
|
+
|
493
|
+
if (!pStream->avail_out)
|
494
|
+
return LZHAM_Z_BUF_ERROR;
|
495
|
+
|
496
|
+
if (flush == LZHAM_Z_PARTIAL_FLUSH)
|
497
|
+
flush = LZHAM_Z_SYNC_FLUSH;
|
498
|
+
|
499
|
+
int lzham_status = LZHAM_Z_OK;
|
500
|
+
lzham_z_ulong orig_total_in = pStream->total_in, orig_total_out = pStream->total_out;
|
501
|
+
for ( ; ; )
|
502
|
+
{
|
503
|
+
size_t in_bytes = pStream->avail_in, out_bytes = pStream->avail_out;
|
504
|
+
|
505
|
+
lzham_compress_state_ptr pComp = (lzham_compress_state_ptr)pStream->state;
|
506
|
+
lzham_compress_state *pState = static_cast<lzham_compress_state*>(pComp);
|
507
|
+
|
508
|
+
lzham_compress_status_t status = lzham_lib_compress2(
|
509
|
+
pComp,
|
510
|
+
pStream->next_in, &in_bytes,
|
511
|
+
pStream->next_out, &out_bytes,
|
512
|
+
(lzham_flush_t)flush);
|
513
|
+
|
514
|
+
pStream->next_in += (uint)in_bytes;
|
515
|
+
pStream->avail_in -= (uint)in_bytes;
|
516
|
+
pStream->total_in += (uint)in_bytes;
|
517
|
+
|
518
|
+
pStream->next_out += (uint)out_bytes;
|
519
|
+
pStream->avail_out -= (uint)out_bytes;
|
520
|
+
pStream->total_out += (uint)out_bytes;
|
521
|
+
|
522
|
+
pStream->adler = pState->m_compressor.get_src_adler32();
|
523
|
+
|
524
|
+
if (status >= LZHAM_COMP_STATUS_FIRST_FAILURE_CODE)
|
525
|
+
{
|
526
|
+
lzham_status = LZHAM_Z_STREAM_ERROR;
|
527
|
+
break;
|
528
|
+
}
|
529
|
+
else if (status == LZHAM_COMP_STATUS_SUCCESS)
|
530
|
+
{
|
531
|
+
lzham_status = LZHAM_Z_STREAM_END;
|
532
|
+
break;
|
533
|
+
}
|
534
|
+
else if (!pStream->avail_out)
|
535
|
+
break;
|
536
|
+
else if ((!pStream->avail_in) && (flush != LZHAM_Z_FINISH))
|
537
|
+
{
|
538
|
+
if ((flush) || (pStream->total_in != orig_total_in) || (pStream->total_out != orig_total_out))
|
539
|
+
break;
|
540
|
+
return LZHAM_Z_BUF_ERROR; // Can't make forward progress without some input.
|
541
|
+
}
|
542
|
+
}
|
543
|
+
return lzham_status;
|
544
|
+
}
|
545
|
+
|
546
|
+
int lzham_lib_z_deflateEnd(lzham_z_streamp pStream)
|
547
|
+
{
|
548
|
+
if (!pStream)
|
549
|
+
return LZHAM_Z_STREAM_ERROR;
|
550
|
+
|
551
|
+
lzham_compress_state_ptr pComp = (lzham_compress_state_ptr)pStream->state;
|
552
|
+
if (pComp)
|
553
|
+
{
|
554
|
+
pStream->adler = lzham_lib_compress_deinit(pComp);
|
555
|
+
|
556
|
+
pStream->state = NULL;
|
557
|
+
}
|
558
|
+
|
559
|
+
return LZHAM_Z_OK;
|
560
|
+
}
|
561
|
+
|
562
|
+
lzham_z_ulong lzham_lib_z_deflateBound(lzham_z_streamp pStream, lzham_z_ulong source_len)
|
563
|
+
{
|
564
|
+
LZHAM_NOTE_UNUSED(pStream);
|
565
|
+
return 64 + source_len + ((source_len + 4095) / 4096) * 4;
|
566
|
+
}
|
567
|
+
|
568
|
+
int lzham_lib_z_compress2(unsigned char *pDest, lzham_z_ulong *pDest_len, const unsigned char *pSource, lzham_z_ulong source_len, int level)
|
569
|
+
{
|
570
|
+
int status;
|
571
|
+
lzham_z_stream stream;
|
572
|
+
memset(&stream, 0, sizeof(stream));
|
573
|
+
|
574
|
+
// In case lzham_z_ulong is 64-bits (argh I hate longs).
|
575
|
+
if ((source_len | *pDest_len) > 0xFFFFFFFFU)
|
576
|
+
return LZHAM_Z_PARAM_ERROR;
|
577
|
+
|
578
|
+
stream.next_in = pSource;
|
579
|
+
stream.avail_in = (uint)source_len;
|
580
|
+
stream.next_out = pDest;
|
581
|
+
stream.avail_out = (uint)*pDest_len;
|
582
|
+
|
583
|
+
status = lzham_lib_z_deflateInit(&stream, level);
|
584
|
+
if (status != LZHAM_Z_OK)
|
585
|
+
return status;
|
586
|
+
|
587
|
+
status = lzham_lib_z_deflate(&stream, LZHAM_Z_FINISH);
|
588
|
+
if (status != LZHAM_Z_STREAM_END)
|
589
|
+
{
|
590
|
+
lzham_lib_z_deflateEnd(&stream);
|
591
|
+
return (status == LZHAM_Z_OK) ? LZHAM_Z_BUF_ERROR : status;
|
592
|
+
}
|
593
|
+
|
594
|
+
*pDest_len = stream.total_out;
|
595
|
+
return lzham_lib_z_deflateEnd(&stream);
|
596
|
+
}
|
597
|
+
|
598
|
+
int lzham_lib_z_compress(unsigned char *pDest, lzham_z_ulong *pDest_len, const unsigned char *pSource, lzham_z_ulong source_len)
|
599
|
+
{
|
600
|
+
return lzham_lib_z_compress2(pDest, pDest_len, pSource, source_len, (int)LZHAM_Z_DEFAULT_COMPRESSION);
|
601
|
+
}
|
602
|
+
|
603
|
+
lzham_z_ulong lzham_lib_z_compressBound(lzham_z_ulong source_len)
|
604
|
+
{
|
605
|
+
return lzham_lib_z_deflateBound(NULL, source_len);
|
606
|
+
}
|
607
|
+
|
608
|
+
} // namespace lzham
|