0001
0002 #include <errno.h>
0003 #include <lzma.h>
0004 #include <stdio.h>
0005 #include <linux/compiler.h>
0006 #include <sys/types.h>
0007 #include <sys/stat.h>
0008 #include <fcntl.h>
0009 #include "compress.h"
0010 #include "debug.h"
0011 #include <string.h>
0012 #include <unistd.h>
0013 #include <internal/lib.h>
0014
0015 #define BUFSIZE 8192
0016
0017 static const char *lzma_strerror(lzma_ret ret)
0018 {
0019 switch ((int) ret) {
0020 case LZMA_MEM_ERROR:
0021 return "Memory allocation failed";
0022 case LZMA_OPTIONS_ERROR:
0023 return "Unsupported decompressor flags";
0024 case LZMA_FORMAT_ERROR:
0025 return "The input is not in the .xz format";
0026 case LZMA_DATA_ERROR:
0027 return "Compressed file is corrupt";
0028 case LZMA_BUF_ERROR:
0029 return "Compressed file is truncated or otherwise corrupt";
0030 default:
0031 return "Unknown error, possibly a bug";
0032 }
0033 }
0034
0035 int lzma_decompress_to_file(const char *input, int output_fd)
0036 {
0037 lzma_action action = LZMA_RUN;
0038 lzma_stream strm = LZMA_STREAM_INIT;
0039 lzma_ret ret;
0040 int err = -1;
0041
0042 u8 buf_in[BUFSIZE];
0043 u8 buf_out[BUFSIZE];
0044 FILE *infile;
0045
0046 infile = fopen(input, "rb");
0047 if (!infile) {
0048 pr_err("lzma: fopen failed on %s: '%s'\n",
0049 input, strerror(errno));
0050 return -1;
0051 }
0052
0053 ret = lzma_stream_decoder(&strm, UINT64_MAX, LZMA_CONCATENATED);
0054 if (ret != LZMA_OK) {
0055 pr_err("lzma: lzma_stream_decoder failed %s (%d)\n",
0056 lzma_strerror(ret), ret);
0057 goto err_fclose;
0058 }
0059
0060 strm.next_in = NULL;
0061 strm.avail_in = 0;
0062 strm.next_out = buf_out;
0063 strm.avail_out = sizeof(buf_out);
0064
0065 while (1) {
0066 if (strm.avail_in == 0 && !feof(infile)) {
0067 strm.next_in = buf_in;
0068 strm.avail_in = fread(buf_in, 1, sizeof(buf_in), infile);
0069
0070 if (ferror(infile)) {
0071 pr_err("lzma: read error: %s\n", strerror(errno));
0072 goto err_lzma_end;
0073 }
0074
0075 if (feof(infile))
0076 action = LZMA_FINISH;
0077 }
0078
0079 ret = lzma_code(&strm, action);
0080
0081 if (strm.avail_out == 0 || ret == LZMA_STREAM_END) {
0082 ssize_t write_size = sizeof(buf_out) - strm.avail_out;
0083
0084 if (writen(output_fd, buf_out, write_size) != write_size) {
0085 pr_err("lzma: write error: %s\n", strerror(errno));
0086 goto err_lzma_end;
0087 }
0088
0089 strm.next_out = buf_out;
0090 strm.avail_out = sizeof(buf_out);
0091 }
0092
0093 if (ret != LZMA_OK) {
0094 if (ret == LZMA_STREAM_END)
0095 break;
0096
0097 pr_err("lzma: failed %s\n", lzma_strerror(ret));
0098 goto err_lzma_end;
0099 }
0100 }
0101
0102 err = 0;
0103 err_lzma_end:
0104 lzma_end(&strm);
0105 err_fclose:
0106 fclose(infile);
0107 return err;
0108 }
0109
0110 bool lzma_is_compressed(const char *input)
0111 {
0112 int fd = open(input, O_RDONLY);
0113 const uint8_t magic[6] = { 0xFD, '7', 'z', 'X', 'Z', 0x00 };
0114 char buf[6] = { 0 };
0115 ssize_t rc;
0116
0117 if (fd < 0)
0118 return -1;
0119
0120 rc = read(fd, buf, sizeof(buf));
0121 close(fd);
0122 return rc == sizeof(buf) ?
0123 memcmp(buf, magic, sizeof(buf)) == 0 : false;
0124 }