1 // SPDX-License-Identifier: GPL-2.0
5 #include <linux/compiler.h>
16 static const char *lzma_strerror(lzma_ret ret
)
20 return "Memory allocation failed";
21 case LZMA_OPTIONS_ERROR
:
22 return "Unsupported decompressor flags";
23 case LZMA_FORMAT_ERROR
:
24 return "The input is not in the .xz format";
26 return "Compressed file is corrupt";
28 return "Compressed file is truncated or otherwise corrupt";
30 return "Unknown error, possibly a bug";
34 int lzma_decompress_to_file(const char *input
, int output_fd
)
36 lzma_action action
= LZMA_RUN
;
37 lzma_stream strm
= LZMA_STREAM_INIT
;
45 infile
= fopen(input
, "rb");
47 pr_err("lzma: fopen failed on %s: '%s'\n",
48 input
, strerror(errno
));
52 ret
= lzma_stream_decoder(&strm
, UINT64_MAX
, LZMA_CONCATENATED
);
54 pr_err("lzma: lzma_stream_decoder failed %s (%d)\n",
55 lzma_strerror(ret
), ret
);
61 strm
.next_out
= buf_out
;
62 strm
.avail_out
= sizeof(buf_out
);
65 if (strm
.avail_in
== 0 && !feof(infile
)) {
66 strm
.next_in
= buf_in
;
67 strm
.avail_in
= fread(buf_in
, 1, sizeof(buf_in
), infile
);
70 pr_err("lzma: read error: %s\n", strerror(errno
));
78 ret
= lzma_code(&strm
, action
);
80 if (strm
.avail_out
== 0 || ret
== LZMA_STREAM_END
) {
81 ssize_t write_size
= sizeof(buf_out
) - strm
.avail_out
;
83 if (writen(output_fd
, buf_out
, write_size
) != write_size
) {
84 pr_err("lzma: write error: %s\n", strerror(errno
));
88 strm
.next_out
= buf_out
;
89 strm
.avail_out
= sizeof(buf_out
);
93 if (ret
== LZMA_STREAM_END
)
96 pr_err("lzma: failed %s\n", lzma_strerror(ret
));
107 bool lzma_is_compressed(const char *input
)
109 int fd
= open(input
, O_RDONLY
);
110 const uint8_t magic
[6] = { 0xFD, '7', 'z', 'X', 'Z', 0x00 };
117 rc
= read(fd
, buf
, sizeof(buf
));
119 return rc
== sizeof(buf
) ?
120 memcmp(buf
, magic
, sizeof(buf
)) == 0 : false;