Repository navigation
Expand file tree
/
Copy pathmz_strm_lzma.c
More file actions
366 lines (293 loc) · 8.94 KB
/
Copy pathmz_strm_lzma.c
File metadata and controls
366 lines (293 loc) · 8.94 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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/* mz_strm_lzma.c -- Stream for lzma inflate/deflate
Version 2.0.1, October 16th, 2017
part of the MiniZip project
Copyright (C) 2012-2017 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
This program is distributed under the terms of the same license as lzma.
See the accompanying LICENSE file for the full text of the license.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <lzma.h>
#include "mz.h"
#include "mz_strm.h"
#include "mz_strm_lzma.h"
/***************************************************************************/
#ifndef DEF_MEM_LEVEL
# if MAX_MEM_LEVEL >= 8
# define DEF_MEM_LEVEL 8
# else
# define DEF_MEM_LEVEL MAX_MEM_LEVEL
# endif
#endif
/***************************************************************************/
mz_stream_vtbl mz_stream_lzma_vtbl = {
mz_stream_lzma_open,
mz_stream_lzma_is_open,
mz_stream_lzma_read,
mz_stream_lzma_write,
mz_stream_lzma_tell,
mz_stream_lzma_seek,
mz_stream_lzma_close,
mz_stream_lzma_error,
mz_stream_lzma_create,
mz_stream_lzma_delete,
mz_stream_lzma_get_prop_int64,
mz_stream_lzma_set_prop_int64
};
/***************************************************************************/
typedef struct mz_stream_lzma_s {
mz_stream stream;
lzma_stream lstream;
uint8_t buffer[INT16_MAX];
int32_t buffer_len;
int64_t total_in;
int64_t total_out;
int64_t max_total_in;
int8_t initialized;
uint32_t preset;
int16_t window_bits;
int16_t mem_level;
int16_t strategy;
int16_t mode;
int16_t error;
} mz_stream_lzma;
/***************************************************************************/
int32_t mz_stream_lzma_open(void *stream, const char *path, int32_t mode)
{
mz_stream_lzma *lzma = (mz_stream_lzma *)stream;
lzma_filter filters[LZMA_FILTERS_MAX + 1];
lzma_options_lzma opt_lzma = { 0 };
uint32_t size = 0;
lzma->lstream.total_in = 0;
lzma->lstream.total_out = 0;
lzma->total_in = 0;
lzma->total_out = 0;
if (mode & MZ_STREAM_MODE_READ)
{
lzma->lstream.next_in = lzma->buffer;
lzma->lstream.avail_in = 0;
lzma->error = lzma_stream_decoder(&lzma->lstream, UINT64_MAX, LZMA_CONCATENATED);
}
else if (mode & MZ_STREAM_MODE_WRITE)
{
lzma->lstream.next_out = lzma->buffer;
lzma->lstream.avail_out = sizeof(lzma->buffer);
if (lzma_lzma_preset(&opt_lzma, lzma->preset))
return MZ_STREAM_ERROR;
memset(&filters, 0, sizeof(filters));
filters[0].id = LZMA_FILTER_LZMA1;
filters[0].options = &opt_lzma;
filters[1].id = LZMA_VLI_UNKNOWN;
lzma_properties_size(&size, (lzma_filter *)&filters);
mz_stream_write_uint8(lzma->stream.base, LZMA_VERSION_MAJOR);
mz_stream_write_uint8(lzma->stream.base, LZMA_VERSION_MINOR);
mz_stream_write_uint16(lzma->stream.base, size);
lzma->total_out += 4;
lzma->error = lzma_alone_encoder(&lzma->lstream, &opt_lzma);
}
if (lzma->error != LZMA_OK)
return MZ_STREAM_ERROR;
lzma->initialized = 1;
lzma->mode = mode;
return MZ_OK;
}
int32_t mz_stream_lzma_is_open(void *stream)
{
mz_stream_lzma *lzma = (mz_stream_lzma *)stream;
if (lzma->initialized != 1)
return MZ_STREAM_ERROR;
return MZ_OK;
}
int32_t mz_stream_lzma_read(void *stream, void *buf, int32_t size)
{
mz_stream_lzma *lzma = (mz_stream_lzma *)stream;
uint64_t total_out_before = 0;
uint64_t total_out_after = 0;
uint32_t out_bytes = 0;
uint32_t total_out = 0;
int32_t bytes_to_read = 0;
int32_t read = 0;
int16_t err = LZMA_OK;
lzma->lstream.next_out = (uint8_t*)buf;
lzma->lstream.avail_out = (uint16_t)size;
do
{
if (lzma->lstream.avail_in == 0)
{
bytes_to_read = sizeof(lzma->buffer);
if (lzma->max_total_in > 0)
{
if ((lzma->max_total_in - lzma->total_in) < sizeof(lzma->buffer))
bytes_to_read = (int32_t)(lzma->max_total_in - lzma->total_in);
}
read = mz_stream_read(lzma->stream.base, lzma->buffer, bytes_to_read);
if (read < 0)
{
lzma->error = MZ_STREAM_ERROR;
break;
}
if (read == 0)
break;
lzma->total_in += read;
lzma->lstream.next_in = lzma->buffer;
lzma->lstream.avail_in = read;
}
total_out_before = lzma->lstream.total_out;
err = lzma_code(&lzma->lstream, LZMA_RUN);
total_out_after = lzma->lstream.total_out;
out_bytes = (int32_t)(total_out_after - total_out_before);
total_out += out_bytes;
if (err == LZMA_STREAM_END)
break;
if (err != LZMA_OK)
{
lzma->error = err;
break;
}
}
while (lzma->lstream.avail_out > 0);
lzma->total_out += total_out;
return total_out;
}
int32_t mz_stream_lzma_flush(void *stream)
{
mz_stream_lzma *lzma = (mz_stream_lzma *)stream;
if (mz_stream_write(lzma->stream.base, lzma->buffer, lzma->buffer_len) != lzma->buffer_len)
return MZ_STREAM_ERROR;
return MZ_OK;
}
uint32_t mz_stream_lzma_code(void *stream, int32_t flush)
{
mz_stream_lzma *lzma = (mz_stream_lzma *)stream;
uint64_t total_out_before = 0;
uint64_t total_out_after = 0;
uint32_t out_bytes = 0;
int16_t err = LZMA_OK;
if (lzma->lstream.avail_out == 0)
{
if (mz_stream_lzma_flush(lzma) != MZ_OK)
{
lzma->error = MZ_STREAM_ERROR;
return MZ_STREAM_ERROR;
}
lzma->lstream.avail_out = sizeof(lzma->buffer);
lzma->lstream.next_out = lzma->buffer;
lzma->buffer_len = 0;
}
total_out_before = lzma->lstream.total_out;
err = lzma_code(&lzma->lstream, flush);
total_out_after = lzma->lstream.total_out;
out_bytes = (uint32_t)(total_out_after - total_out_before);
if (err != LZMA_OK && err != LZMA_STREAM_END)
{
lzma->error = err;
return MZ_STREAM_ERROR;
}
lzma->buffer_len += out_bytes;
lzma->total_out += out_bytes;
return MZ_OK;
}
int32_t mz_stream_lzma_write(void *stream, const void *buf, int32_t size)
{
mz_stream_lzma *lzma = (mz_stream_lzma *)stream;
lzma->lstream.next_in = (uint8_t*)buf;
lzma->lstream.avail_in = size;
while ((lzma->error == LZMA_OK) && (lzma->lstream.avail_in > 0))
mz_stream_lzma_code(stream, LZMA_RUN);
lzma->total_in += size;
return size;
}
int64_t mz_stream_lzma_tell(void *stream)
{
return MZ_STREAM_ERROR;
}
int32_t mz_stream_lzma_seek(void *stream, int64_t offset, int32_t origin)
{
return MZ_STREAM_ERROR;
}
int32_t mz_stream_lzma_close(void *stream)
{
mz_stream_lzma *lzma = (mz_stream_lzma *)stream;
if (lzma->mode & MZ_STREAM_MODE_READ)
{
lzma_end(&lzma->lstream);
}
else if (lzma->mode & MZ_STREAM_MODE_WRITE)
{
mz_stream_lzma_code(stream, LZMA_FINISH);
mz_stream_lzma_flush(stream);
lzma_end(&lzma->lstream);
}
lzma->initialized = 0;
if (lzma->error != LZMA_OK)
return MZ_STREAM_ERROR;
return MZ_OK;
}
int32_t mz_stream_lzma_error(void *stream)
{
mz_stream_lzma *lzma = (mz_stream_lzma *)stream;
return lzma->error;
}
int32_t mz_stream_lzma_get_prop_int64(void *stream, int32_t prop, int64_t *value)
{
mz_stream_lzma *lzma = (mz_stream_lzma *)stream;
switch (prop)
{
case MZ_STREAM_PROP_TOTAL_IN:
*value = lzma->total_in;
return MZ_OK;
case MZ_STREAM_PROP_TOTAL_OUT:
*value = lzma->total_out;
return MZ_OK;
}
return MZ_EXIST_ERROR;
}
int32_t mz_stream_lzma_set_prop_int64(void *stream, int32_t prop, int64_t value)
{
mz_stream_lzma *lzma = (mz_stream_lzma *)stream;
switch (prop)
{
case MZ_STREAM_PROP_COMPRESS_LEVEL:
if (value >= 9)
lzma->preset = LZMA_PRESET_EXTREME;
else
lzma->preset = LZMA_PRESET_DEFAULT;
return MZ_OK;
case MZ_STREAM_PROP_TOTAL_IN_MAX:
lzma->max_total_in = value;
return MZ_OK;
}
return MZ_EXIST_ERROR;
}
void *mz_stream_lzma_create(void **stream)
{
mz_stream_lzma *lzma = NULL;
lzma = (mz_stream_lzma *)malloc(sizeof(mz_stream_lzma));
if (lzma != NULL)
{
memset(lzma, 0, sizeof(mz_stream_lzma));
lzma->stream.vtbl = &mz_stream_lzma_vtbl;
lzma->preset = LZMA_PRESET_DEFAULT;
}
if (stream != NULL)
*stream = lzma;
return lzma;
}
void mz_stream_lzma_delete(void **stream)
{
mz_stream_lzma *lzma = NULL;
if (stream == NULL)
return;
lzma = (mz_stream_lzma *)*stream;
if (lzma != NULL)
free(lzma);
*stream = NULL;
}
void *mz_stream_lzma_get_interface(void)
{
return (void *)&mz_stream_lzma_vtbl;
}