unxz.c revision 1.1 1 1.1 christos
2 1.1 christos #include <stdarg.h>
3 1.1 christos #include <errno.h>
4 1.1 christos #include <stdio.h>
5 1.1 christos #include <unistd.h>
6 1.1 christos #include <lzma.h>
7 1.1 christos
8 1.1 christos static off_t
9 1.1 christos unxz(int i, int o, char *pre, size_t prelen, off_t *bytes_in)
10 1.1 christos {
11 1.1 christos lzma_stream strm = LZMA_STREAM_INIT;
12 1.1 christos lzma_ret ret;
13 1.1 christos off_t x = 0;
14 1.1 christos
15 1.1 christos // Initialize the decoder
16 1.1 christos ret = lzma_alone_decoder(&strm, UINT64_MAX);
17 1.1 christos if (ret != LZMA_OK) {
18 1.1 christos errno = ret == LZMA_MEM_ERROR ? ENOMEM : EINVAL;
19 1.1 christos maybe_errx("Cannot initialize decoder");
20 1.1 christos }
21 1.1 christos
22 1.1 christos // Input and output buffers
23 1.1 christos uint8_t ibuf[BUFSIZ];
24 1.1 christos uint8_t obuf[BUFSIZ];
25 1.1 christos
26 1.1 christos *bytes_in = prelen;
27 1.1 christos strm.next_in = ibuf;
28 1.1 christos strm.avail_in = read(i, ibuf + prelen, sizeof(ibuf) - prelen);
29 1.1 christos if (strm.avail_in == (size_t)-1)
30 1.1 christos maybe_errx("Read failed");
31 1.1 christos
32 1.1 christos memcpy(ibuf, pre, prelen);
33 1.1 christos *bytes_in += strm.avail_in;
34 1.1 christos
35 1.1 christos strm.next_out = obuf;
36 1.1 christos strm.avail_out = sizeof(obuf);
37 1.1 christos if ((ret = lzma_stream_decoder(&strm, UINT64_MAX, 0)) != LZMA_OK)
38 1.1 christos maybe_errx("Can't initialize decoder");
39 1.1 christos
40 1.1 christos for (;;) {
41 1.1 christos if (strm.avail_in == 0) {
42 1.1 christos strm.next_in = ibuf;
43 1.1 christos strm.avail_in = read(i, ibuf, sizeof(ibuf));
44 1.1 christos // fprintf(stderr, "read = %zu\n", strm.avail_in);
45 1.1 christos if (strm.avail_in == (size_t)-1)
46 1.1 christos maybe_errx("Read failed");
47 1.1 christos }
48 1.1 christos
49 1.1 christos ret = lzma_code(&strm, LZMA_RUN);
50 1.1 christos // fprintf(stderr, "ret = %d %zu %zu\n", ret, strm.avail_in, strm.avail_out);
51 1.1 christos
52 1.1 christos // Write and check write error before checking decoder error.
53 1.1 christos // This way as much data as possible gets written to output
54 1.1 christos // even if decoder detected an error.
55 1.1 christos if (strm.avail_out == 0 || ret != LZMA_OK) {
56 1.1 christos const size_t write_size = sizeof(obuf) - strm.avail_out;
57 1.1 christos
58 1.1 christos if (write(o, obuf, write_size) != (ssize_t)write_size)
59 1.1 christos maybe_err("write failed");
60 1.1 christos
61 1.1 christos strm.next_out = obuf;
62 1.1 christos strm.avail_out = sizeof(obuf);
63 1.1 christos x += write_size;
64 1.1 christos }
65 1.1 christos
66 1.1 christos if (ret != LZMA_OK) {
67 1.1 christos if (ret == LZMA_STREAM_END) {
68 1.1 christos // Check that there's no trailing garbage.
69 1.1 christos if (strm.avail_in != 0 || read(i, ibuf, 1))
70 1.1 christos ret = LZMA_DATA_ERROR;
71 1.1 christos else {
72 1.1 christos lzma_end(&strm);
73 1.1 christos return x;
74 1.1 christos }
75 1.1 christos }
76 1.1 christos
77 1.1 christos const char *msg;
78 1.1 christos switch (ret) {
79 1.1 christos case LZMA_MEM_ERROR:
80 1.1 christos msg = strerror(ENOMEM);
81 1.1 christos break;
82 1.1 christos
83 1.1 christos case LZMA_FORMAT_ERROR:
84 1.1 christos msg = "File format not recognized";
85 1.1 christos break;
86 1.1 christos
87 1.1 christos case LZMA_OPTIONS_ERROR:
88 1.1 christos // FIXME: Better message?
89 1.1 christos msg = "Unsupported compression options";
90 1.1 christos break;
91 1.1 christos
92 1.1 christos case LZMA_DATA_ERROR:
93 1.1 christos msg = "File is corrupt";
94 1.1 christos break;
95 1.1 christos
96 1.1 christos case LZMA_BUF_ERROR:
97 1.1 christos msg = "Unexpected end of input";
98 1.1 christos break;
99 1.1 christos
100 1.1 christos case LZMA_MEMLIMIT_ERROR:
101 1.1 christos msg = "Reached memory limit";
102 1.1 christos break;
103 1.1 christos
104 1.1 christos
105 1.1 christos default:
106 1.1 christos msg = "Internal error (bug)";
107 1.1 christos break;
108 1.1 christos }
109 1.1 christos
110 1.1 christos maybe_errx("%s", msg);
111 1.1 christos }
112 1.1 christos }
113 1.1 christos }
114