90 lzma_ret ret = lzma_easy_encoder(strm, preset, LZMA_CHECK_CRC64);
109 msg =
"Memory allocation failed";
112 case LZMA_OPTIONS_ERROR:
113 msg =
"Specified preset is not supported";
116 case LZMA_UNSUPPORTED_CHECK:
117 msg =
"Specified integrity check is not supported";
120 case LZMA_FORMAT_ERROR:
122 msg =
"The input is not in the .xz format";
125 case LZMA_DATA_ERROR:
138 msg =
"File size limits exceeded";
148 msg =
"Compressed file is truncated or " 164 msg =
"Unknown error, possibly a bug";
172 compress(lzma_stream *strm, FILE *infile, FILE *outfile)
176 lzma_action action = LZMA_RUN;
180 uint8_t
inbuf[BUFSIZ];
181 uint8_t outbuf[BUFSIZ];
194 strm->next_in =
NULL;
196 strm->next_out = outbuf;
197 strm->avail_out =
sizeof(outbuf);
203 if (strm->avail_in == 0 && !feof(infile)) {
204 strm->next_in =
inbuf;
205 strm->avail_in =
fread(inbuf, 1,
sizeof(inbuf),
208 if (ferror(infile)) {
209 return LZMA_PROG_ERROR;
217 action = LZMA_FINISH;
235 lzma_ret ret = lzma_code(strm, action);
240 if (strm->avail_out == 0 || ret == LZMA_STREAM_END) {
245 size_t write_size =
sizeof(outbuf) - strm->avail_out;
247 if (
fwrite(outbuf, 1, write_size, outfile)
249 return LZMA_PROG_ERROR;
253 strm->next_out = outbuf;
254 strm->avail_out =
sizeof(outbuf);
259 if (ret != LZMA_OK) {
266 if (ret == LZMA_STREAM_END)
295 lzma_action action = LZMA_RUN;
297 uint8_t
inbuf[BUFSIZ];
298 uint8_t outbuf[BUFSIZ];
300 strm->next_in =
NULL;
302 strm->next_out = outbuf;
303 strm->avail_out =
sizeof(outbuf);
306 if (strm->avail_in == 0 && !feof(infile)) {
307 strm->next_in =
inbuf;
308 strm->avail_in =
fread(inbuf, 1,
sizeof(inbuf),
311 if (ferror(infile)) {
312 return LZMA_PROG_ERROR;
321 action = LZMA_FINISH;
324 lzma_ret ret = lzma_code(strm, action);
326 if (strm->avail_out == 0 || ret == LZMA_STREAM_END) {
327 size_t write_size =
sizeof(outbuf) - strm->avail_out;
329 if (
fwrite(outbuf, 1, write_size, outfile)
331 return LZMA_BUF_ERROR;
334 strm->next_out = outbuf;
335 strm->avail_out =
sizeof(outbuf);
338 if (ret != LZMA_OK) {
347 if (ret == LZMA_STREAM_END)
392 lzma_ret ret = lzma_stream_decoder(
393 strm, UINT64_MAX, LZMA_CONCATENATED);
426 lzma_stream strm = LZMA_STREAM_INIT;
432 ret =
compress(&strm, source, dest);
460 lzma_stream strm = LZMA_STREAM_INIT;
463 if (ret != LZMA_OK) {
478 #ifdef __UNIT_TEST_COMPRESS__ 482 int main(
int argc,
char *argv[])
485 printf(
"syntax: %s input_compressed_file output_file\n", argv[0]);
488 FILE *fp_in = fopen(argv[1],
"r");
489 FILE *fp_out = fopen(argv[2],
"wx");
491 if (fp_in ==
NULL || fp_out ==
NULL) {
492 perror(
"fail to open file:");
497 printf(
"cannot compress %s into %s\n", argv[1], argv[2]);
498 perror(
"compress fail:");
504 fp_out = fopen(argv[2],
"r");
505 FILE *fp_def = tmpfile();
508 printf(
"cannot decompress %s\n", argv[2]);
509 perror(
"compress fail:");
514 fseek(fp_def, 0, SEEK_SET);
515 fgets(buffer, 10, fp_def);
517 printf(
"file: '%s'\n", buffer);
enum compress_e compress_deflate(FILE *source, FILE *dest, int level)
enum compress_e compress_inflate(FILE *source, FILE *dest)
static lzma_ret init_encoder(lzma_stream *strm, uint32_t preset)
static lzma_ret init_decoder(lzma_stream *strm)
size_t MONITOR_EXT_WRAP_NAME() fread(void *ptr, size_t size, size_t count, FILE *stream)
int main(int argc, char *argv[])
static lzma_ret compress(lzma_stream *strm, FILE *infile, FILE *outfile)
static const char * get_error_message(lzma_ret ret) __attribute__((unused))
size_t MONITOR_EXT_WRAP_NAME() fwrite(const void *ptr, size_t size, size_t count, FILE *stream)
static lzma_ret decompress(lzma_stream *strm, FILE *infile, FILE *outfile)