1 /* pxzcat derived from nbdkit
2 * Copyright (C) 2013 Red Hat Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * * Neither the name of Red Hat nor the names of its contributors may be
17 * used to endorse or promote products derived from this software without
18 * specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
27 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 #include <sys/types.h>
53 #define debug(fs,...) fprintf (stderr, "pxzcat: debug: " fs, ## __VA_ARGS__)
55 #define debug(fs,...) /* nothing */
58 #define XZ_HEADER_MAGIC "\xfd" "7zXZ\0"
59 #define XZ_HEADER_MAGIC_LEN 6
60 #define XZ_FOOTER_MAGIC "YZ"
61 #define XZ_FOOTER_MAGIC_LEN 2
63 static void usage (int exitcode);
64 static void xzfile_uncompress (const char *filename, const char *outputfile);
65 static int check_header_magic (int fd);
66 static lzma_index *parse_indexes (const char *filename, int fd);
67 static void iter_indexes (lzma_index *idx);
69 static struct option long_options[] = {
70 { "output", required_argument, 0, 'o' },
71 { "help", 0, 0, '?' },
75 static const char *options = "o:";
78 main (int argc, char *argv[])
82 const char *outputfile = NULL;
85 c = getopt_long (argc, argv, options, long_options, &longopt_index);
90 /* Long option with no short opt equivalent. */
102 usage (EXIT_FAILURE);
106 if (outputfile == NULL)
107 error (EXIT_FAILURE, 0, "you must give the -o (output file) option\n");
109 if (optind != argc - 1)
110 usage (EXIT_FAILURE);
112 xzfile_uncompress (argv[optind], outputfile);
120 printf ("usage: pxzcat -o output input.xz\n");
125 xzfile_uncompress (const char *filename, const char *outputfile)
132 fd = open (filename, O_RDONLY);
134 error (EXIT_FAILURE, errno, "open: %s", filename);
136 /* Check file magic. */
137 if (!check_header_magic (fd))
138 error (EXIT_FAILURE, 0, "%s: not an xz file", filename);
140 /* Read and parse the indexes. */
141 idx = parse_indexes (filename, fd);
143 /* Iterate over indexes and uncompress. */
150 check_header_magic (int fd)
152 char buf[XZ_HEADER_MAGIC_LEN];
154 if (lseek (fd, 0, SEEK_SET) == -1)
156 if (read (fd, buf, XZ_HEADER_MAGIC_LEN) != XZ_HEADER_MAGIC_LEN)
158 if (memcmp (buf, XZ_HEADER_MAGIC, XZ_HEADER_MAGIC_LEN) != 0)
163 /* For explanation of this function, see src/xz/list.c:parse_indexes
167 parse_indexes (const char *filename, int fd)
170 off_t pos, index_size;
171 uint8_t footer[LZMA_STREAM_HEADER_SIZE];
172 uint8_t header[LZMA_STREAM_HEADER_SIZE];
173 lzma_stream_flags footer_flags;
174 lzma_stream_flags header_flags;
175 lzma_stream strm = LZMA_STREAM_INIT;
177 lzma_index *combined_index = NULL;
178 lzma_index *this_index = NULL;
179 lzma_vli stream_padding = 0;
180 size_t nr_streams = 0;
182 /* Check file size is a multiple of 4 bytes. */
183 pos = lseek (fd, 0, SEEK_END);
184 if (pos == (off_t) -1)
185 error (EXIT_FAILURE, errno, "%s: lseek", filename);
188 error (EXIT_FAILURE, 0,
189 "%s: not an xz file: size is not a multiple of 4 bytes",
192 /* Jump backwards through the file identifying each stream. */
194 debug ("looping through streams: pos = %" PRIu64, (uint64_t) pos);
196 if (pos < LZMA_STREAM_HEADER_SIZE)
197 error (EXIT_FAILURE, 0,
198 "%s: corrupted file at %" PRIu64, filename, (uint64_t) pos);
200 if (lseek (fd, -LZMA_STREAM_HEADER_SIZE, SEEK_CUR) == -1)
201 error (EXIT_FAILURE, errno, "%s: lseek", filename);
203 if (read (fd, footer, LZMA_STREAM_HEADER_SIZE) != LZMA_STREAM_HEADER_SIZE)
204 error (EXIT_FAILURE, errno, "%s: read stream footer", filename);
206 /* Skip stream padding. */
207 if (footer[8] == 0 && footer[9] == 0 &&
208 footer[10] == 0 && footer[11] == 0) {
214 pos -= LZMA_STREAM_HEADER_SIZE;
217 debug ("decode stream footer at pos = %" PRIu64, (uint64_t) pos);
219 /* Does the stream footer look reasonable? */
220 r = lzma_stream_footer_decode (&footer_flags, footer);
222 error (EXIT_FAILURE, 0,
223 "%s: invalid stream footer (error %d)", filename, r);
225 debug ("backward_size = %" PRIu64, (uint64_t) footer_flags.backward_size);
226 index_size = footer_flags.backward_size;
227 if (pos < index_size + LZMA_STREAM_HEADER_SIZE)
228 error (EXIT_FAILURE, 0, "%s: invalid stream footer", filename);
231 debug ("decode index at pos = %" PRIu64, (uint64_t) pos);
233 /* Seek backwards to the index of this stream. */
234 if (lseek (fd, pos, SEEK_SET) == -1)
235 error (EXIT_FAILURE, errno, "%s: lseek", filename);
237 /* Decode the index. */
238 r = lzma_index_decoder (&strm, &this_index, UINT64_MAX);
240 error (EXIT_FAILURE, 0,
241 "%s: invalid stream index (error %d)", filename, r);
246 strm.avail_in = index_size;
247 if (strm.avail_in > BUFSIZ)
248 strm.avail_in = BUFSIZ;
250 n = read (fd, &buf, strm.avail_in);
252 error (EXIT_FAILURE, errno, "%s: read", filename);
254 index_size -= strm.avail_in;
257 r = lzma_code (&strm, LZMA_RUN);
258 } while (r == LZMA_OK);
260 if (r != LZMA_STREAM_END)
261 error (EXIT_FAILURE, 0, "%s: could not parse index (error %d)",
264 pos -= lzma_index_total_size (this_index) + LZMA_STREAM_HEADER_SIZE;
266 debug ("decode stream header at pos = %" PRIu64, (uint64_t) pos);
268 /* Read and decode the stream header. */
269 if (lseek (fd, pos, SEEK_SET) == -1)
270 error (EXIT_FAILURE, errno, "%s: lseek", filename);
272 if (read (fd, header, LZMA_STREAM_HEADER_SIZE) != LZMA_STREAM_HEADER_SIZE)
273 error (EXIT_FAILURE, errno, "%s: read stream header", filename);
275 r = lzma_stream_header_decode (&header_flags, header);
277 error (EXIT_FAILURE, 0,
278 "%s: invalid stream header (error %d)", filename, r);
280 /* Header and footer of the stream should be equal. */
281 r = lzma_stream_flags_compare (&header_flags, &footer_flags);
283 error (EXIT_FAILURE, 0,
284 "%s: header and footer of stream are not equal (error %d)",
287 /* Store the decoded stream flags in this_index. */
288 r = lzma_index_stream_flags (this_index, &footer_flags);
290 error (EXIT_FAILURE, 0,
291 "%s: cannot read stream_flags from index (error %d)",
294 /* Store the amount of stream padding so far. Needed to calculate
295 * compressed offsets correctly in multi-stream files.
297 r = lzma_index_stream_padding (this_index, stream_padding);
299 error (EXIT_FAILURE, 0,
300 "%s: cannot set stream_padding in index (error %d)",
303 if (combined_index != NULL) {
304 r = lzma_index_cat (this_index, combined_index, NULL);
306 error (EXIT_FAILURE, 0, "%s: cannot combine indexes", filename);
309 combined_index = this_index;
315 return combined_index;
318 /* Iterate over the indexes and uncompress.
321 iter_indexes (lzma_index *idx)
323 lzma_index_iter iter;
325 lzma_index_iter_init (&iter, idx);
326 while (!lzma_index_iter_next (&iter, LZMA_INDEX_ITER_NONEMPTY_BLOCK)) {
337 xzfile_read_block (xzfile *xz, uint64_t offset,
338 uint64_t *start_rtn, uint64_t *size_rtn)
340 lzma_index_iter iter;
341 uint8_t header[LZMA_BLOCK_HEADER_SIZE_MAX];
343 lzma_filter filters[LZMA_FILTERS_MAX + 1];
345 lzma_stream strm = LZMA_STREAM_INIT;
350 /* Locate the block containing the uncompressed offset. */
351 lzma_index_iter_init (&iter, xz->idx);
352 if (lzma_index_iter_locate (&iter, offset)) {
353 nbdkit_error ("cannot find offset %" PRIu64 " in the xz file", offset);
357 *start_rtn = iter.block.uncompressed_file_offset;
358 *size_rtn = iter.block.uncompressed_size;
360 nbdkit_debug ("seek: block number %d at file offset %" PRIu64,
361 (int) iter.block.number_in_file,
362 (uint64_t) iter.block.compressed_file_offset);
364 if (lseek (xz->fd, iter.block.compressed_file_offset, SEEK_SET) == -1) {
365 nbdkit_error ("lseek: %m");
369 /* Read the block header. Start by reading a single byte which
370 * tell us how big the block header is.
372 n = read (xz->fd, header, 1);
374 nbdkit_error ("read: unexpected end of file reading block header byte");
378 nbdkit_error ("read: %m");
382 if (header[0] == '\0') {
383 nbdkit_error ("read: unexpected invalid block in file, header[0] = 0");
388 block.check = iter.stream.flags->check;
389 block.filters = filters;
390 block.header_size = lzma_block_header_size_decode (header[0]);
392 /* Now read and decode the block header. */
393 n = read (xz->fd, &header[1], block.header_size-1);
394 if (n >= 0 && n != block.header_size-1) {
395 nbdkit_error ("read: unexpected end of file reading block header");
399 nbdkit_error ("read: %m");
403 r = lzma_block_header_decode (&block, NULL, header);
405 nbdkit_error ("invalid block header (error %d)", r);
409 /* What this actually does is it checks that the block header
412 r = lzma_block_compressed_size (&block, iter.block.unpadded_size);
414 nbdkit_error ("cannot calculate compressed size (error %d)", r);
418 /* Read the block data. */
419 r = lzma_block_decoder (&strm, &block);
421 nbdkit_error ("invalid block (error %d)", r);
425 data = malloc (*size_rtn);
427 nbdkit_error ("malloc (%zu bytes): %m\n"
428 "NOTE: If this error occurs, you need to recompress your xz files with a smaller block size. Use: 'xz --block-size=16777216 ...'.",
435 strm.next_out = (uint8_t *) data;
436 strm.avail_out = block.uncompressed_size;
440 lzma_action action = LZMA_RUN;
442 if (strm.avail_in == 0) {
444 n = read (xz->fd, buf, sizeof buf);
446 nbdkit_error ("read: %m");
451 action = LZMA_FINISH;
456 r = lzma_code (&strm, action);
457 } while (r == LZMA_OK);
459 if (r != LZMA_OK && r != LZMA_STREAM_END) {
460 nbdkit_error ("could not parse block data (error %d)", r);
466 for (i = 0; filters[i].id != LZMA_VLI_UNKNOWN; ++i)
467 free (filters[i].options);
475 for (i = 0; filters[i].id != LZMA_VLI_UNKNOWN; ++i)
476 free (filters[i].options);