/usr.bin/gzip/unxz.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 153 lines · 96 code · 22 blank · 35 comment · 27 complexity · 28f8d62555bda3f7bcf3fd0e2c1ec54a MD5 · raw file

  1. /* $NetBSD: unxz.c,v 1.5 2011/09/30 01:32:21 christos Exp $ */
  2. /*-
  3. * Copyright (c) 2011 The NetBSD Foundation, Inc.
  4. * All rights reserved.
  5. *
  6. * This code is derived from software contributed to The NetBSD Foundation
  7. * by Christos Zoulas.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  19. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  20. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  21. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  22. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include <sys/cdefs.h>
  31. __FBSDID("$FreeBSD$");
  32. #include <stdarg.h>
  33. #include <errno.h>
  34. #include <stdio.h>
  35. #include <unistd.h>
  36. #include <lzma.h>
  37. static off_t
  38. unxz(int i, int o, char *pre, size_t prelen, off_t *bytes_in)
  39. {
  40. lzma_stream strm = LZMA_STREAM_INIT;
  41. static const int flags = LZMA_TELL_UNSUPPORTED_CHECK|LZMA_CONCATENATED;
  42. lzma_ret ret;
  43. lzma_action action = LZMA_RUN;
  44. off_t bytes_out, bp;
  45. uint8_t ibuf[BUFSIZ];
  46. uint8_t obuf[BUFSIZ];
  47. if (bytes_in == NULL)
  48. bytes_in = &bp;
  49. strm.next_in = ibuf;
  50. memcpy(ibuf, pre, prelen);
  51. strm.avail_in = read(i, ibuf + prelen, sizeof(ibuf) - prelen);
  52. if (strm.avail_in == (size_t)-1)
  53. maybe_err("read failed");
  54. strm.avail_in += prelen;
  55. *bytes_in = strm.avail_in;
  56. if ((ret = lzma_stream_decoder(&strm, UINT64_MAX, flags)) != LZMA_OK)
  57. maybe_errx("Can't initialize decoder (%d)", ret);
  58. strm.next_out = NULL;
  59. strm.avail_out = 0;
  60. if ((ret = lzma_code(&strm, LZMA_RUN)) != LZMA_OK)
  61. maybe_errx("Can't read headers (%d)", ret);
  62. bytes_out = 0;
  63. strm.next_out = obuf;
  64. strm.avail_out = sizeof(obuf);
  65. for (;;) {
  66. if (strm.avail_in == 0) {
  67. strm.next_in = ibuf;
  68. strm.avail_in = read(i, ibuf, sizeof(ibuf));
  69. switch (strm.avail_in) {
  70. case (size_t)-1:
  71. maybe_err("read failed");
  72. /*NOTREACHED*/
  73. case 0:
  74. action = LZMA_FINISH;
  75. break;
  76. default:
  77. *bytes_in += strm.avail_in;
  78. break;
  79. }
  80. }
  81. ret = lzma_code(&strm, action);
  82. // Write and check write error before checking decoder error.
  83. // This way as much data as possible gets written to output
  84. // even if decoder detected an error.
  85. if (strm.avail_out == 0 || ret != LZMA_OK) {
  86. const size_t write_size = sizeof(obuf) - strm.avail_out;
  87. if (write(o, obuf, write_size) != (ssize_t)write_size)
  88. maybe_err("write failed");
  89. strm.next_out = obuf;
  90. strm.avail_out = sizeof(obuf);
  91. bytes_out += write_size;
  92. }
  93. if (ret != LZMA_OK) {
  94. if (ret == LZMA_STREAM_END) {
  95. // Check that there's no trailing garbage.
  96. if (strm.avail_in != 0 || read(i, ibuf, 1))
  97. ret = LZMA_DATA_ERROR;
  98. else {
  99. lzma_end(&strm);
  100. return bytes_out;
  101. }
  102. }
  103. const char *msg;
  104. switch (ret) {
  105. case LZMA_MEM_ERROR:
  106. msg = strerror(ENOMEM);
  107. break;
  108. case LZMA_FORMAT_ERROR:
  109. msg = "File format not recognized";
  110. break;
  111. case LZMA_OPTIONS_ERROR:
  112. // FIXME: Better message?
  113. msg = "Unsupported compression options";
  114. break;
  115. case LZMA_DATA_ERROR:
  116. msg = "File is corrupt";
  117. break;
  118. case LZMA_BUF_ERROR:
  119. msg = "Unexpected end of input";
  120. break;
  121. case LZMA_MEMLIMIT_ERROR:
  122. msg = "Reached memory limit";
  123. break;
  124. default:
  125. maybe_errx("Unknown error (%d)", ret);
  126. break;
  127. }
  128. maybe_errx("%s", msg);
  129. }
  130. }
  131. }