lzma.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <errno.h>
  3. #include <lzma.h>
  4. #include <stdio.h>
  5. #include <linux/compiler.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include "compress.h"
  10. #include "debug.h"
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <internal/lib.h>
  14. #define BUFSIZE 8192
  15. static const char *lzma_strerror(lzma_ret ret)
  16. {
  17. switch ((int) ret) {
  18. case LZMA_MEM_ERROR:
  19. return "Memory allocation failed";
  20. case LZMA_OPTIONS_ERROR:
  21. return "Unsupported decompressor flags";
  22. case LZMA_FORMAT_ERROR:
  23. return "The input is not in the .xz format";
  24. case LZMA_DATA_ERROR:
  25. return "Compressed file is corrupt";
  26. case LZMA_BUF_ERROR:
  27. return "Compressed file is truncated or otherwise corrupt";
  28. default:
  29. return "Unknown error, possibly a bug";
  30. }
  31. }
  32. int lzma_decompress_to_file(const char *input, int output_fd)
  33. {
  34. lzma_action action = LZMA_RUN;
  35. lzma_stream strm = LZMA_STREAM_INIT;
  36. lzma_ret ret;
  37. int err = -1;
  38. u8 buf_in[BUFSIZE];
  39. u8 buf_out[BUFSIZE];
  40. FILE *infile;
  41. infile = fopen(input, "rb");
  42. if (!infile) {
  43. pr_err("lzma: fopen failed on %s: '%s'\n",
  44. input, strerror(errno));
  45. return -1;
  46. }
  47. ret = lzma_stream_decoder(&strm, UINT64_MAX, LZMA_CONCATENATED);
  48. if (ret != LZMA_OK) {
  49. pr_err("lzma: lzma_stream_decoder failed %s (%d)\n",
  50. lzma_strerror(ret), ret);
  51. goto err_fclose;
  52. }
  53. strm.next_in = NULL;
  54. strm.avail_in = 0;
  55. strm.next_out = buf_out;
  56. strm.avail_out = sizeof(buf_out);
  57. while (1) {
  58. if (strm.avail_in == 0 && !feof(infile)) {
  59. strm.next_in = buf_in;
  60. strm.avail_in = fread(buf_in, 1, sizeof(buf_in), infile);
  61. if (ferror(infile)) {
  62. pr_err("lzma: read error: %s\n", strerror(errno));
  63. goto err_lzma_end;
  64. }
  65. if (feof(infile))
  66. action = LZMA_FINISH;
  67. }
  68. ret = lzma_code(&strm, action);
  69. if (strm.avail_out == 0 || ret == LZMA_STREAM_END) {
  70. ssize_t write_size = sizeof(buf_out) - strm.avail_out;
  71. if (writen(output_fd, buf_out, write_size) != write_size) {
  72. pr_err("lzma: write error: %s\n", strerror(errno));
  73. goto err_lzma_end;
  74. }
  75. strm.next_out = buf_out;
  76. strm.avail_out = sizeof(buf_out);
  77. }
  78. if (ret != LZMA_OK) {
  79. if (ret == LZMA_STREAM_END)
  80. break;
  81. pr_err("lzma: failed %s\n", lzma_strerror(ret));
  82. goto err_lzma_end;
  83. }
  84. }
  85. err = 0;
  86. err_lzma_end:
  87. lzma_end(&strm);
  88. err_fclose:
  89. fclose(infile);
  90. return err;
  91. }
  92. bool lzma_is_compressed(const char *input)
  93. {
  94. int fd = open(input, O_RDONLY);
  95. const uint8_t magic[6] = { 0xFD, '7', 'z', 'X', 'Z', 0x00 };
  96. char buf[6] = { 0 };
  97. ssize_t rc;
  98. if (fd < 0)
  99. return -1;
  100. rc = read(fd, buf, sizeof(buf));
  101. close(fd);
  102. return rc == sizeof(buf) ?
  103. memcmp(buf, magic, sizeof(buf)) == 0 : false;
  104. }