-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxz.c
242 lines (212 loc) · 7.99 KB
/
xz.c
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
/* Copyright (c) 2020, 2022 Dennis Wölfing
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* xz.c
* XZ compression.
*/
#include <config.h>
#include <string.h>
#include <unistd.h>
#include "algorithm.h"
#if WITH_LIBLZMA
# include <lzma.h>
#endif
static int xzCompress(int input, int output, int level, struct fileinfo* info);
static int xzDecompress(int input, int output, struct fileinfo* info,
const unsigned char* buffer, size_t bufferSize);
static bool xzProbe(const unsigned char* buffer, size_t bufferSize);
const struct algorithm algoXz = {
.names = "xz",
.extensions = "xz,txz:tar",
.minLevel = 0,
.defaultLevel = 6,
.maxLevel = 9,
.compress = xzCompress,
.decompress = xzDecompress,
.probe = xzProbe
};
#define BUFFER_SIZE (4096 * 8)
#define XZMAGIC "\xfd""7zXZ"
static bool xzProbe(const unsigned char* buffer, size_t bufferSize) {
return bufferSize >= 6 && memcmp(buffer, XZMAGIC, 6) == 0;
}
#if WITH_LIBLZMA
static lzma_ret createEncoder(lzma_stream* stream, int level) {
#if HAVE_LZMA_STREAM_ENCODER_MT
lzma_mt mt = {0};
mt.preset = level;
mt.check = LZMA_CHECK_CRC64;
if (maxThreads > 0) {
mt.threads = maxThreads;
} else {
mt.threads = lzma_cputhreads();
}
if (maxThreads == -1) {
// When the -T option was not given we still want to use multiple
// threads but we should limit the number of threads to avoid high
// memory usage. We try to limit our memory usage to one third of the
// available memory.
uint64_t memoryAvailable = lzma_physmem();
uint64_t memoryUsage = lzma_stream_encoder_mt_memusage(&mt);
while (memoryUsage > memoryAvailable / 3 && mt.threads > 1) {
mt.threads--;
memoryUsage = lzma_stream_encoder_mt_memusage(&mt);
}
}
if (mt.threads > 1) {
if (lzma_stream_encoder_mt(stream, &mt) == LZMA_OK) {
return LZMA_OK;
}
}
#endif
return lzma_easy_encoder(stream, level, LZMA_CHECK_CRC64);
}
#endif
static int xzCompress(int input, int output, int level, struct fileinfo* info) {
#if WITH_LIBLZMA
lzma_stream stream = LZMA_STREAM_INIT;
lzma_ret status = createEncoder(&stream, level);
if (status == LZMA_MEM_ERROR) return RESULT_OUT_OF_MEMORY;
if (status != LZMA_OK) return RESULT_UNKNOWN_ERROR;
unsigned char inputBuffer[BUFFER_SIZE];
unsigned char outputBuffer[BUFFER_SIZE];
stream.next_out = outputBuffer;
stream.avail_out = sizeof(outputBuffer);
while (true) {
if (stream.avail_out == 0) {
if (writeAll(output, outputBuffer, sizeof(outputBuffer)) < 0) {
lzma_end(&stream);
return RESULT_WRITE_ERROR;
}
stream.next_out = outputBuffer;
stream.avail_out = sizeof(outputBuffer);
}
if (stream.avail_in == 0) {
ssize_t bytesRead = read(input, inputBuffer, sizeof(inputBuffer));
if (bytesRead < 0) {
lzma_end(&stream);
return RESULT_READ_ERROR;
}
stream.next_in = inputBuffer;
stream.avail_in = bytesRead;
if (bytesRead == 0) break;
}
status = lzma_code(&stream, LZMA_RUN);
if (status != LZMA_OK) {
lzma_end(&stream);
// LZMA_DATA_ERROR means the data exceeds the maximum possible size.
return status == LZMA_MEM_ERROR ? RESULT_OUT_OF_MEMORY :
status == LZMA_DATA_ERROR ? RESULT_FORMAT_ERROR :
RESULT_UNKNOWN_ERROR;
}
}
do {
status = lzma_code(&stream, LZMA_FINISH);
if (status != LZMA_OK && status != LZMA_STREAM_END) {
lzma_end(&stream);
return status == LZMA_MEM_ERROR ? RESULT_OUT_OF_MEMORY :
status == LZMA_DATA_ERROR ? RESULT_FORMAT_ERROR :
RESULT_UNKNOWN_ERROR;
}
if (writeAll(output, outputBuffer,
sizeof(outputBuffer) - stream.avail_out) < 0) {
lzma_end(&stream);
return RESULT_WRITE_ERROR;
}
stream.next_out = outputBuffer;
stream.avail_out = sizeof(outputBuffer);
} while (status != LZMA_STREAM_END);
info->uncompressedSize = stream.total_in;
info->compressedSize = stream.total_out;
lzma_end(&stream);
return RESULT_OK;
#else
(void) input; (void) output; (void) level; (void) info;
return RESULT_UNIMPLEMENTED_FORMAT;
#endif
}
static int xzDecompress(int input, int output, struct fileinfo* info,
const unsigned char* buffer, size_t bufferSize) {
#if WITH_LIBLZMA
if (output == -2) {
output = openOutputFile(NULL, info->oinfo);
if (output < 0) return RESULT_OPEN_FAILURE;
}
lzma_stream stream = LZMA_STREAM_INIT;
lzma_ret status = lzma_stream_decoder(&stream, UINT64_MAX,
LZMA_CONCATENATED);
if (status == LZMA_MEM_ERROR) return RESULT_OUT_OF_MEMORY;
if (status != LZMA_OK) return RESULT_UNKNOWN_ERROR;
unsigned char inputBuffer[BUFFER_SIZE];
unsigned char outputBuffer[BUFFER_SIZE];
memcpy(inputBuffer, buffer, bufferSize);
stream.next_in = inputBuffer;
stream.avail_in = bufferSize;
stream.next_out = outputBuffer;
stream.avail_out = sizeof(outputBuffer);
while (true) {
if (stream.avail_out == 0) {
if (writeAll(output, outputBuffer, sizeof(outputBuffer)) < 0) {
lzma_end(&stream);
return RESULT_WRITE_ERROR;
}
stream.next_out = outputBuffer;
stream.avail_out = sizeof(outputBuffer);
}
if (stream.avail_in == 0) {
ssize_t bytesRead = read(input, inputBuffer, sizeof(inputBuffer));
if (bytesRead < 0) {
lzma_end(&stream);
return RESULT_READ_ERROR;
}
stream.next_in = inputBuffer;
stream.avail_in = bytesRead;
if (bytesRead == 0) break;
}
status = lzma_code(&stream, LZMA_RUN);
if (status != LZMA_OK) {
lzma_end(&stream);
return status == LZMA_MEM_ERROR ? RESULT_OUT_OF_MEMORY :
status == LZMA_FORMAT_ERROR ? RESULT_FORMAT_ERROR :
status == LZMA_DATA_ERROR ? RESULT_FORMAT_ERROR :
RESULT_UNKNOWN_ERROR;
}
}
do {
status = lzma_code(&stream, LZMA_FINISH);
if (status != LZMA_OK && status != LZMA_STREAM_END) {
lzma_end(&stream);
return status == LZMA_MEM_ERROR ? RESULT_OUT_OF_MEMORY :
status == LZMA_FORMAT_ERROR ? RESULT_FORMAT_ERROR :
status == LZMA_DATA_ERROR ? RESULT_FORMAT_ERROR :
RESULT_UNKNOWN_ERROR;
}
if (writeAll(output, outputBuffer,
sizeof(outputBuffer) - stream.avail_out) < 0) {
lzma_end(&stream);
return RESULT_WRITE_ERROR;
}
stream.next_out = outputBuffer;
stream.avail_out = sizeof(outputBuffer);
} while (status != LZMA_STREAM_END);
info->compressedSize = stream.total_in;
info->uncompressedSize = stream.total_out;
info->crc = -1;
lzma_end(&stream);
return RESULT_OK;
#else
(void) input; (void) output; (void) info; (void) buffer; (void) bufferSize;
return RESULT_UNIMPLEMENTED_FORMAT;
#endif
}