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