1 // SPDX-License-Identifier: GPL-2.0
5 #include <linux/compiler.h>
12 static const char *lzma_strerror(lzma_ret ret
)
16 return "Memory allocation failed";
17 case LZMA_OPTIONS_ERROR
:
18 return "Unsupported decompressor flags";
19 case LZMA_FORMAT_ERROR
:
20 return "The input is not in the .xz format";
22 return "Compressed file is corrupt";
24 return "Compressed file is truncated or otherwise corrupt";
26 return "Unknown error, possibly a bug";
30 int lzma_decompress_to_file(const char *input
, int output_fd
)
32 lzma_action action
= LZMA_RUN
;
33 lzma_stream strm
= LZMA_STREAM_INIT
;
41 infile
= fopen(input
, "rb");
43 pr_err("lzma: fopen failed on %s: '%s'\n",
44 input
, strerror(errno
));
48 ret
= lzma_stream_decoder(&strm
, UINT64_MAX
, LZMA_CONCATENATED
);
50 pr_err("lzma: lzma_stream_decoder failed %s (%d)\n",
51 lzma_strerror(ret
), ret
);
57 strm
.next_out
= buf_out
;
58 strm
.avail_out
= sizeof(buf_out
);
61 if (strm
.avail_in
== 0 && !feof(infile
)) {
62 strm
.next_in
= buf_in
;
63 strm
.avail_in
= fread(buf_in
, 1, sizeof(buf_in
), infile
);
66 pr_err("lzma: read error: %s\n", strerror(errno
));
74 ret
= lzma_code(&strm
, action
);
76 if (strm
.avail_out
== 0 || ret
== LZMA_STREAM_END
) {
77 ssize_t write_size
= sizeof(buf_out
) - strm
.avail_out
;
79 if (writen(output_fd
, buf_out
, write_size
) != write_size
) {
80 pr_err("lzma: write error: %s\n", strerror(errno
));
84 strm
.next_out
= buf_out
;
85 strm
.avail_out
= sizeof(buf_out
);
89 if (ret
== LZMA_STREAM_END
)
92 pr_err("lzma: failed %s\n", lzma_strerror(ret
));