1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief Listing information about .xz files
6 // Author: Lasse Collin
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
14 #include "tuklib_integer.h"
17 /// Information about a .xz file
19 /// Combined Index of all Streams in the file
22 /// Total amount of Stream Padding
23 uint64_t stream_padding;
25 /// Highest memory usage so far
26 uint64_t memusage_max;
28 /// True if all Blocks so far have Compressed Size and
29 /// Uncompressed Size fields
34 #define XZ_FILE_INFO_INIT { NULL, 0, 0, true }
37 /// Information about a .xz Block
39 /// Size of the Block Header
42 /// A few of the Block Flags as a string
45 /// Size of the Compressed Data field in the Block
46 lzma_vli compressed_size;
48 /// Decoder memory usage for this Block
51 /// The filter chain of this Block in human-readable form
52 char filter_chain[FILTERS_STR_SIZE];
57 /// Check ID to string mapping
58 static const char check_names[LZMA_CHECK_ID_MAX + 1][12] = {
59 // TRANSLATORS: Indicates that there is no integrity check.
60 // This string is used in tables, so the width must not
61 // exceed ten columns with a fixed-width font.
64 // TRANSLATORS: Indicates that integrity check name is not known,
65 // but the Check ID is known (here 2). This and other "Unknown-N"
66 // strings are used in tables, so the width must not exceed ten
67 // columns with a fixed-width font. It's OK to omit the dash if
68 // you need space for one extra letter, but don't use spaces.
85 /// Buffer size for get_check_names(). This may be a bit ridiculous,
86 /// but at least it's enough if some language needs many multibyte chars.
87 #define CHECKS_STR_SIZE 1024
90 /// Value of the Check field as hexadecimal string.
91 /// This is set by parse_check_value().
92 static char check_value[2 * LZMA_CHECK_SIZE_MAX + 1];
95 /// Totals that are displayed if there was more than one file.
96 /// The "files" counter is also used in print_info_adv() to show
102 uint64_t compressed_size;
103 uint64_t uncompressed_size;
104 uint64_t stream_padding;
105 uint64_t memusage_max;
108 } totals = { 0, 0, 0, 0, 0, 0, 0, 0, true };
111 /// \brief Parse the Index(es) from the given .xz file
113 /// \param xfi Pointer to structure where the decoded information
115 /// \param pair Input file
117 /// \return On success, false is returned. On error, true is returned.
119 // TODO: This function is pretty big. liblzma should have a function that
120 // takes a callback function to parse the Index(es) from a .xz file to make
121 // it easy for applications.
123 parse_indexes(xz_file_info *xfi, file_pair *pair)
125 if (pair->src_st.st_size <= 0) {
126 message_error(_("%s: File is empty"), pair->src_name);
130 if (pair->src_st.st_size < 2 * LZMA_STREAM_HEADER_SIZE) {
131 message_error(_("%s: Too small to be a valid .xz file"),
137 lzma_stream_flags header_flags;
138 lzma_stream_flags footer_flags;
141 // lzma_stream for the Index decoder
142 lzma_stream strm = LZMA_STREAM_INIT;
144 // All Indexes decoded so far
145 lzma_index *combined_index = NULL;
147 // The Index currently being decoded
148 lzma_index *this_index = NULL;
150 // Current position in the file. We parse the file backwards so
151 // initialize it to point to the end of the file.
152 off_t pos = pair->src_st.st_size;
154 // Each loop iteration decodes one Index.
156 // Check that there is enough data left to contain at least
157 // the Stream Header and Stream Footer. This check cannot
158 // fail in the first pass of this loop.
159 if (pos < 2 * LZMA_STREAM_HEADER_SIZE) {
160 message_error("%s: %s", pair->src_name,
161 message_strm(LZMA_DATA_ERROR));
165 pos -= LZMA_STREAM_HEADER_SIZE;
166 lzma_vli stream_padding = 0;
168 // Locate the Stream Footer. There may be Stream Padding which
169 // we must skip when reading backwards.
171 if (pos < LZMA_STREAM_HEADER_SIZE) {
172 message_error("%s: %s", pair->src_name,
178 if (io_pread(pair, &buf,
179 LZMA_STREAM_HEADER_SIZE, pos))
182 // Stream Padding is always a multiple of four bytes.
187 // To avoid calling io_pread() for every four bytes
188 // of Stream Padding, take advantage that we read
189 // 12 bytes (LZMA_STREAM_HEADER_SIZE) already and
190 // check them too before calling io_pread() again.
195 } while (i >= 0 && buf.u32[i] == 0);
198 // Decode the Stream Footer.
199 ret = lzma_stream_footer_decode(&footer_flags, buf.u8);
200 if (ret != LZMA_OK) {
201 message_error("%s: %s", pair->src_name,
206 // Check that the size of the Index field looks sane.
207 lzma_vli index_size = footer_flags.backward_size;
208 if ((lzma_vli)(pos) < index_size + LZMA_STREAM_HEADER_SIZE) {
209 message_error("%s: %s", pair->src_name,
210 message_strm(LZMA_DATA_ERROR));
214 // Set pos to the beginning of the Index.
217 // See how much memory we can use for decoding this Index.
218 uint64_t memlimit = hardware_memlimit_get(MODE_LIST);
219 uint64_t memused = 0;
220 if (combined_index != NULL) {
221 memused = lzma_index_memused(combined_index);
222 if (memused > memlimit)
229 ret = lzma_index_decoder(&strm, &this_index, memlimit);
230 if (ret != LZMA_OK) {
231 message_error("%s: %s", pair->src_name,
237 // Don't give the decoder more input than the
239 strm.avail_in = my_min(IO_BUFFER_SIZE, index_size);
240 if (io_pread(pair, &buf, strm.avail_in, pos))
243 pos += strm.avail_in;
244 index_size -= strm.avail_in;
246 strm.next_in = buf.u8;
247 ret = lzma_code(&strm, LZMA_RUN);
249 } while (ret == LZMA_OK);
251 // If the decoding seems to be successful, check also that
252 // the Index decoder consumed as much input as indicated
253 // by the Backward Size field.
254 if (ret == LZMA_STREAM_END)
255 if (index_size != 0 || strm.avail_in != 0)
256 ret = LZMA_DATA_ERROR;
258 if (ret != LZMA_STREAM_END) {
259 // LZMA_BUFFER_ERROR means that the Index decoder
260 // would have liked more input than what the Index
261 // size should be according to Stream Footer.
262 // The message for LZMA_DATA_ERROR makes more
263 // sense in that case.
264 if (ret == LZMA_BUF_ERROR)
265 ret = LZMA_DATA_ERROR;
267 message_error("%s: %s", pair->src_name,
270 // If the error was too low memory usage limit,
271 // show also how much memory would have been needed.
272 if (ret == LZMA_MEMLIMIT_ERROR) {
273 uint64_t needed = lzma_memusage(&strm);
274 if (UINT64_MAX - needed < memused)
279 message_mem_needed(V_ERROR, needed);
285 // Decode the Stream Header and check that its Stream Flags
286 // match the Stream Footer.
287 pos -= footer_flags.backward_size + LZMA_STREAM_HEADER_SIZE;
288 if ((lzma_vli)(pos) < lzma_index_total_size(this_index)) {
289 message_error("%s: %s", pair->src_name,
290 message_strm(LZMA_DATA_ERROR));
294 pos -= lzma_index_total_size(this_index);
295 if (io_pread(pair, &buf, LZMA_STREAM_HEADER_SIZE, pos))
298 ret = lzma_stream_header_decode(&header_flags, buf.u8);
299 if (ret != LZMA_OK) {
300 message_error("%s: %s", pair->src_name,
305 ret = lzma_stream_flags_compare(&header_flags, &footer_flags);
306 if (ret != LZMA_OK) {
307 message_error("%s: %s", pair->src_name,
312 // Store the decoded Stream Flags into this_index. This is
313 // needed so that we can print which Check is used in each
315 ret = lzma_index_stream_flags(this_index, &footer_flags);
319 // Store also the size of the Stream Padding field. It is
320 // needed to show the offsets of the Streams correctly.
321 ret = lzma_index_stream_padding(this_index, stream_padding);
325 if (combined_index != NULL) {
326 // Append the earlier decoded Indexes
328 ret = lzma_index_cat(
329 this_index, combined_index, NULL);
330 if (ret != LZMA_OK) {
331 message_error("%s: %s", pair->src_name,
337 combined_index = this_index;
340 xfi->stream_padding += stream_padding;
346 // All OK. Make combined_index available to the caller.
347 xfi->idx = combined_index;
351 // Something went wrong, free the allocated memory.
353 lzma_index_end(combined_index, NULL);
354 lzma_index_end(this_index, NULL);
359 /// \brief Parse the Block Header
361 /// The result is stored into *bhi. The caller takes care of initializing it.
363 /// \return False on success, true on error.
365 parse_block_header(file_pair *pair, const lzma_index_iter *iter,
366 block_header_info *bhi, xz_file_info *xfi)
368 #if IO_BUFFER_SIZE < LZMA_BLOCK_HEADER_SIZE_MAX
369 # error IO_BUFFER_SIZE < LZMA_BLOCK_HEADER_SIZE_MAX
372 // Get the whole Block Header with one read, but don't read past
373 // the end of the Block (or even its Check field).
374 const uint32_t size = my_min(iter->block.total_size
375 - lzma_check_size(iter->stream.flags->check),
376 LZMA_BLOCK_HEADER_SIZE_MAX);
378 if (io_pread(pair, &buf, size, iter->block.compressed_file_offset))
381 // Zero would mean Index Indicator and thus not a valid Block.
385 // Initialize the block structure and decode Block Header Size.
386 lzma_filter filters[LZMA_FILTERS_MAX + 1];
389 block.check = iter->stream.flags->check;
390 block.filters = filters;
392 block.header_size = lzma_block_header_size_decode(buf.u8[0]);
393 if (block.header_size > size)
396 // Decode the Block Header.
397 switch (lzma_block_header_decode(&block, NULL, buf.u8)) {
401 case LZMA_OPTIONS_ERROR:
402 message_error("%s: %s", pair->src_name,
403 message_strm(LZMA_OPTIONS_ERROR));
406 case LZMA_DATA_ERROR:
413 // Check the Block Flags. These must be done before calling
414 // lzma_block_compressed_size(), because it overwrites
415 // block.compressed_size.
416 bhi->flags[0] = block.compressed_size != LZMA_VLI_UNKNOWN
418 bhi->flags[1] = block.uncompressed_size != LZMA_VLI_UNKNOWN
420 bhi->flags[2] = '\0';
422 // Collect information if all Blocks have both Compressed Size
423 // and Uncompressed Size fields. They can be useful e.g. for
424 // multi-threaded decompression so it can be useful to know it.
425 xfi->all_have_sizes &= block.compressed_size != LZMA_VLI_UNKNOWN
426 && block.uncompressed_size != LZMA_VLI_UNKNOWN;
428 // Validate or set block.compressed_size.
429 switch (lzma_block_compressed_size(&block,
430 iter->block.unpadded_size)) {
434 case LZMA_DATA_ERROR:
435 // Free the memory allocated by lzma_block_header_decode().
436 for (size_t i = 0; filters[i].id != LZMA_VLI_UNKNOWN; ++i)
437 free(filters[i].options);
445 // Copy the known sizes.
446 bhi->header_size = block.header_size;
447 bhi->compressed_size = block.compressed_size;
449 // Calculate the decoder memory usage and update the maximum
450 // memory usage of this Block.
451 bhi->memusage = lzma_raw_decoder_memusage(filters);
452 if (xfi->memusage_max < bhi->memusage)
453 xfi->memusage_max = bhi->memusage;
455 // Convert the filter chain to human readable form.
456 message_filters_to_str(bhi->filter_chain, filters, false);
458 // Free the memory allocated by lzma_block_header_decode().
459 for (size_t i = 0; filters[i].id != LZMA_VLI_UNKNOWN; ++i)
460 free(filters[i].options);
465 // Show the error message.
466 message_error("%s: %s", pair->src_name,
467 message_strm(LZMA_DATA_ERROR));
472 /// \brief Parse the Check field and put it into check_value[]
474 /// \return False on success, true on error.
476 parse_check_value(file_pair *pair, const lzma_index_iter *iter)
478 // Don't read anything from the file if there is no integrity Check.
479 if (iter->stream.flags->check == LZMA_CHECK_NONE) {
480 snprintf(check_value, sizeof(check_value), "---");
484 // Locate and read the Check field.
485 const uint32_t size = lzma_check_size(iter->stream.flags->check);
486 const off_t offset = iter->block.compressed_file_offset
487 + iter->block.total_size - size;
489 if (io_pread(pair, &buf, size, offset))
492 // CRC32 and CRC64 are in little endian. Guess that all the future
493 // 32-bit and 64-bit Check values are little endian too. It shouldn't
494 // be a too big problem if this guess is wrong.
496 snprintf(check_value, sizeof(check_value),
497 "%08" PRIx32, conv32le(buf.u32[0]));
499 snprintf(check_value, sizeof(check_value),
500 "%016" PRIx64, conv64le(buf.u64[0]));
502 for (size_t i = 0; i < size; ++i)
503 snprintf(check_value + i * 2, 3, "%02x", buf.u8[i]);
509 /// \brief Parse detailed information about a Block
511 /// Since this requires seek(s), listing information about all Blocks can
514 /// \param pair Input file
515 /// \param iter Location of the Block whose Check value should
517 /// \param bhi Pointer to structure where to store the information
518 /// about the Block Header field.
520 /// \return False on success, true on error. If an error occurs,
521 /// the error message is printed too so the caller doesn't
522 /// need to worry about that.
524 parse_details(file_pair *pair, const lzma_index_iter *iter,
525 block_header_info *bhi, xz_file_info *xfi)
527 if (parse_block_header(pair, iter, bhi, xfi))
530 if (parse_check_value(pair, iter))
537 /// \brief Get the compression ratio
539 /// This has slightly different format than that is used in message.c.
541 get_ratio(uint64_t compressed_size, uint64_t uncompressed_size)
543 if (uncompressed_size == 0)
546 const double ratio = (double)(compressed_size)
547 / (double)(uncompressed_size);
552 snprintf(buf, sizeof(buf), "%.3f", ratio);
557 /// \brief Get a comma-separated list of Check names
559 /// The check names are translated with gettext except when in robot mode.
561 /// \param buf Buffer to hold the resulting string
562 /// \param checks Bit mask of Checks to print
563 /// \param space_after_comma
564 /// It's better to not use spaces in table-like listings,
565 /// but in more verbose formats a space after a comma
566 /// is good for readability.
568 get_check_names(char buf[CHECKS_STR_SIZE],
569 uint32_t checks, bool space_after_comma)
574 size_t left = CHECKS_STR_SIZE;
576 const char *sep = space_after_comma ? ", " : ",";
579 for (size_t i = 0; i <= LZMA_CHECK_ID_MAX; ++i) {
580 if (checks & (UINT32_C(1) << i)) {
581 my_snprintf(&pos, &left, "%s%s",
583 opt_robot ? check_names[i]
584 : _(check_names[i]));
594 print_info_basic(const xz_file_info *xfi, file_pair *pair)
596 static bool headings_displayed = false;
597 if (!headings_displayed) {
598 headings_displayed = true;
599 // TRANSLATORS: These are column headings. From Strms (Streams)
600 // to Ratio, the columns are right aligned. Check and Filename
601 // are left aligned. If you need longer words, it's OK to
602 // use two lines here. Test with "xz -l foo.xz".
603 puts(_("Strms Blocks Compressed Uncompressed Ratio "
607 char checks[CHECKS_STR_SIZE];
608 get_check_names(checks, lzma_index_checks(xfi->idx), false);
610 const char *cols[7] = {
611 uint64_to_str(lzma_index_stream_count(xfi->idx), 0),
612 uint64_to_str(lzma_index_block_count(xfi->idx), 1),
613 uint64_to_nicestr(lzma_index_file_size(xfi->idx),
614 NICESTR_B, NICESTR_TIB, false, 2),
615 uint64_to_nicestr(lzma_index_uncompressed_size(xfi->idx),
616 NICESTR_B, NICESTR_TIB, false, 3),
617 get_ratio(lzma_index_file_size(xfi->idx),
618 lzma_index_uncompressed_size(xfi->idx)),
622 printf("%*s %*s %*s %*s %*s %-*s %s\n",
623 tuklib_mbstr_fw(cols[0], 5), cols[0],
624 tuklib_mbstr_fw(cols[1], 7), cols[1],
625 tuklib_mbstr_fw(cols[2], 11), cols[2],
626 tuklib_mbstr_fw(cols[3], 11), cols[3],
627 tuklib_mbstr_fw(cols[4], 5), cols[4],
628 tuklib_mbstr_fw(cols[5], 7), cols[5],
636 print_adv_helper(uint64_t stream_count, uint64_t block_count,
637 uint64_t compressed_size, uint64_t uncompressed_size,
638 uint32_t checks, uint64_t stream_padding)
640 char checks_str[CHECKS_STR_SIZE];
641 get_check_names(checks_str, checks, true);
643 printf(_(" Streams: %s\n"),
644 uint64_to_str(stream_count, 0));
645 printf(_(" Blocks: %s\n"),
646 uint64_to_str(block_count, 0));
647 printf(_(" Compressed size: %s\n"),
648 uint64_to_nicestr(compressed_size,
649 NICESTR_B, NICESTR_TIB, true, 0));
650 printf(_(" Uncompressed size: %s\n"),
651 uint64_to_nicestr(uncompressed_size,
652 NICESTR_B, NICESTR_TIB, true, 0));
653 printf(_(" Ratio: %s\n"),
654 get_ratio(compressed_size, uncompressed_size));
655 printf(_(" Check: %s\n"), checks_str);
656 printf(_(" Stream padding: %s\n"),
657 uint64_to_nicestr(stream_padding,
658 NICESTR_B, NICESTR_TIB, true, 0));
664 print_info_adv(xz_file_info *xfi, file_pair *pair)
666 // Print the overall information.
667 print_adv_helper(lzma_index_stream_count(xfi->idx),
668 lzma_index_block_count(xfi->idx),
669 lzma_index_file_size(xfi->idx),
670 lzma_index_uncompressed_size(xfi->idx),
671 lzma_index_checks(xfi->idx),
672 xfi->stream_padding);
674 // Size of the biggest Check. This is used to calculate the width
675 // of the CheckVal field. The table would get insanely wide if
676 // we always reserved space for 64-byte Check (128 chars as hex).
677 uint32_t check_max = 0;
679 // Print information about the Streams.
681 // TRANSLATORS: The second line is column headings. All except
682 // Check are right aligned; Check is left aligned. Test with
684 puts(_(" Streams:\n Stream Blocks"
685 " CompOffset UncompOffset"
686 " CompSize UncompSize Ratio"
689 lzma_index_iter iter;
690 lzma_index_iter_init(&iter, xfi->idx);
692 while (!lzma_index_iter_next(&iter, LZMA_INDEX_ITER_STREAM)) {
693 const char *cols1[4] = {
694 uint64_to_str(iter.stream.number, 0),
695 uint64_to_str(iter.stream.block_count, 1),
696 uint64_to_str(iter.stream.compressed_offset, 2),
697 uint64_to_str(iter.stream.uncompressed_offset, 3),
699 printf(" %*s %*s %*s %*s ",
700 tuklib_mbstr_fw(cols1[0], 6), cols1[0],
701 tuklib_mbstr_fw(cols1[1], 9), cols1[1],
702 tuklib_mbstr_fw(cols1[2], 15), cols1[2],
703 tuklib_mbstr_fw(cols1[3], 15), cols1[3]);
705 const char *cols2[5] = {
706 uint64_to_str(iter.stream.compressed_size, 0),
707 uint64_to_str(iter.stream.uncompressed_size, 1),
708 get_ratio(iter.stream.compressed_size,
709 iter.stream.uncompressed_size),
710 _(check_names[iter.stream.flags->check]),
711 uint64_to_str(iter.stream.padding, 2),
713 printf("%*s %*s %*s %-*s %*s\n",
714 tuklib_mbstr_fw(cols2[0], 15), cols2[0],
715 tuklib_mbstr_fw(cols2[1], 15), cols2[1],
716 tuklib_mbstr_fw(cols2[2], 5), cols2[2],
717 tuklib_mbstr_fw(cols2[3], 10), cols2[3],
718 tuklib_mbstr_fw(cols2[4], 7), cols2[4]);
720 // Update the maximum Check size.
721 if (lzma_check_size(iter.stream.flags->check) > check_max)
722 check_max = lzma_check_size(iter.stream.flags->check);
725 // Cache the verbosity level to a local variable.
726 const bool detailed = message_verbosity_get() >= V_DEBUG;
728 // Information collected from Block Headers
729 block_header_info bhi;
731 // Print information about the Blocks but only if there is
732 // at least one Block.
733 if (lzma_index_block_count(xfi->idx) > 0) {
734 // Calculate the width of the CheckVal field.
735 const int checkval_width = my_max(8, 2 * check_max);
737 // TRANSLATORS: The second line is column headings. All
738 // except Check are right aligned; Check is left aligned.
739 printf(_(" Blocks:\n Stream Block"
740 " CompOffset UncompOffset"
741 " TotalSize UncompSize Ratio Check"));
744 // TRANSLATORS: These are additional column headings
745 // for the most verbose listing mode. CheckVal
746 // (Check value), Flags, and Filters are left aligned.
747 // Header (Block Header Size), CompSize, and MemUsage
748 // are right aligned. %*s is replaced with 0-120
749 // spaces to make the CheckVal column wide enough.
750 // Test with "xz -lvv foo.xz".
751 printf(_(" CheckVal %*s Header Flags "
752 "CompSize MemUsage Filters"),
753 checkval_width - 8, "");
758 lzma_index_iter_init(&iter, xfi->idx);
760 // Iterate over the Blocks.
761 while (!lzma_index_iter_next(&iter, LZMA_INDEX_ITER_BLOCK)) {
762 if (detailed && parse_details(pair, &iter, &bhi, xfi))
765 const char *cols1[4] = {
766 uint64_to_str(iter.stream.number, 0),
768 iter.block.number_in_stream, 1),
770 iter.block.compressed_file_offset, 2),
772 iter.block.uncompressed_file_offset, 3)
774 printf(" %*s %*s %*s %*s ",
775 tuklib_mbstr_fw(cols1[0], 6), cols1[0],
776 tuklib_mbstr_fw(cols1[1], 9), cols1[1],
777 tuklib_mbstr_fw(cols1[2], 15), cols1[2],
778 tuklib_mbstr_fw(cols1[3], 15), cols1[3]);
780 const char *cols2[4] = {
781 uint64_to_str(iter.block.total_size, 0),
782 uint64_to_str(iter.block.uncompressed_size,
784 get_ratio(iter.block.total_size,
785 iter.block.uncompressed_size),
786 _(check_names[iter.stream.flags->check])
788 printf("%*s %*s %*s %-*s",
789 tuklib_mbstr_fw(cols2[0], 15), cols2[0],
790 tuklib_mbstr_fw(cols2[1], 15), cols2[1],
791 tuklib_mbstr_fw(cols2[2], 5), cols2[2],
792 tuklib_mbstr_fw(cols2[3], detailed ? 11 : 1),
796 const lzma_vli compressed_size
797 = iter.block.unpadded_size
800 iter.stream.flags->check);
802 const char *cols3[6] = {
804 uint64_to_str(bhi.header_size, 0),
806 uint64_to_str(compressed_size, 1),
808 round_up_to_mib(bhi.memusage),
812 // Show MiB for memory usage, because it
813 // is the only size which is not in bytes.
814 printf("%-*s %*s %-5s %*s %*s MiB %s",
815 checkval_width, cols3[0],
816 tuklib_mbstr_fw(cols3[1], 6), cols3[1],
818 tuklib_mbstr_fw(cols3[3], 15),
820 tuklib_mbstr_fw(cols3[4], 7), cols3[4],
829 printf(_(" Memory needed: %s MiB\n"), uint64_to_str(
830 round_up_to_mib(xfi->memusage_max), 0));
831 printf(_(" Sizes in headers: %s\n"),
832 xfi->all_have_sizes ? _("Yes") : _("No"));
840 print_info_robot(xz_file_info *xfi, file_pair *pair)
842 char checks[CHECKS_STR_SIZE];
843 get_check_names(checks, lzma_index_checks(xfi->idx), false);
845 printf("name\t%s\n", pair->src_name);
847 printf("file\t%" PRIu64 "\t%" PRIu64 "\t%" PRIu64 "\t%" PRIu64
848 "\t%s\t%s\t%" PRIu64 "\n",
849 lzma_index_stream_count(xfi->idx),
850 lzma_index_block_count(xfi->idx),
851 lzma_index_file_size(xfi->idx),
852 lzma_index_uncompressed_size(xfi->idx),
853 get_ratio(lzma_index_file_size(xfi->idx),
854 lzma_index_uncompressed_size(xfi->idx)),
856 xfi->stream_padding);
858 if (message_verbosity_get() >= V_VERBOSE) {
859 lzma_index_iter iter;
860 lzma_index_iter_init(&iter, xfi->idx);
862 while (!lzma_index_iter_next(&iter, LZMA_INDEX_ITER_STREAM))
863 printf("stream\t%" PRIu64 "\t%" PRIu64 "\t%" PRIu64
864 "\t%" PRIu64 "\t%" PRIu64 "\t%" PRIu64
865 "\t%s\t%s\t%" PRIu64 "\n",
867 iter.stream.block_count,
868 iter.stream.compressed_offset,
869 iter.stream.uncompressed_offset,
870 iter.stream.compressed_size,
871 iter.stream.uncompressed_size,
872 get_ratio(iter.stream.compressed_size,
873 iter.stream.uncompressed_size),
874 check_names[iter.stream.flags->check],
875 iter.stream.padding);
877 lzma_index_iter_rewind(&iter);
878 block_header_info bhi;
880 while (!lzma_index_iter_next(&iter, LZMA_INDEX_ITER_BLOCK)) {
881 if (message_verbosity_get() >= V_DEBUG
883 pair, &iter, &bhi, xfi))
886 printf("block\t%" PRIu64 "\t%" PRIu64 "\t%" PRIu64
887 "\t%" PRIu64 "\t%" PRIu64
888 "\t%" PRIu64 "\t%" PRIu64 "\t%s\t%s",
890 iter.block.number_in_stream,
891 iter.block.number_in_file,
892 iter.block.compressed_file_offset,
893 iter.block.uncompressed_file_offset,
894 iter.block.total_size,
895 iter.block.uncompressed_size,
896 get_ratio(iter.block.total_size,
897 iter.block.uncompressed_size),
898 check_names[iter.stream.flags->check]);
900 if (message_verbosity_get() >= V_DEBUG)
901 printf("\t%s\t%" PRIu32 "\t%s\t%" PRIu64
914 if (message_verbosity_get() >= V_DEBUG)
915 printf("summary\t%" PRIu64 "\t%s\n",
917 xfi->all_have_sizes ? "yes" : "no");
924 update_totals(const xz_file_info *xfi)
926 // TODO: Integer overflow checks
928 totals.streams += lzma_index_stream_count(xfi->idx);
929 totals.blocks += lzma_index_block_count(xfi->idx);
930 totals.compressed_size += lzma_index_file_size(xfi->idx);
931 totals.uncompressed_size += lzma_index_uncompressed_size(xfi->idx);
932 totals.stream_padding += xfi->stream_padding;
933 totals.checks |= lzma_index_checks(xfi->idx);
935 if (totals.memusage_max < xfi->memusage_max)
936 totals.memusage_max = xfi->memusage_max;
938 totals.all_have_sizes &= xfi->all_have_sizes;
945 print_totals_basic(void)
947 // Print a separator line.
949 memset(line, '-', sizeof(line));
950 line[sizeof(line) - 1] = '\0';
953 // Get the check names.
954 char checks[CHECKS_STR_SIZE];
955 get_check_names(checks, totals.checks, false);
957 // Print the totals except the file count, which needs
959 printf("%5s %7s %11s %11s %5s %-7s ",
960 uint64_to_str(totals.streams, 0),
961 uint64_to_str(totals.blocks, 1),
962 uint64_to_nicestr(totals.compressed_size,
963 NICESTR_B, NICESTR_TIB, false, 2),
964 uint64_to_nicestr(totals.uncompressed_size,
965 NICESTR_B, NICESTR_TIB, false, 3),
966 get_ratio(totals.compressed_size,
967 totals.uncompressed_size),
970 // Since we print totals only when there are at least two files,
971 // the English message will always use "%s files". But some other
972 // languages need different forms for different plurals so we
973 // have to translate this with ngettext().
975 // TRANSLATORS: %s is an integer. Only the plural form of this
976 // message is used (e.g. "2 files"). Test with "xz -l foo.xz bar.xz".
977 printf(ngettext("%s file\n", "%s files\n",
978 totals.files <= ULONG_MAX ? totals.files
979 : (totals.files % 1000000) + 1000000),
980 uint64_to_str(totals.files, 0));
987 print_totals_adv(void)
991 printf(_(" Number of files: %s\n"),
992 uint64_to_str(totals.files, 0));
993 print_adv_helper(totals.streams, totals.blocks,
994 totals.compressed_size, totals.uncompressed_size,
995 totals.checks, totals.stream_padding);
997 if (message_verbosity_get() >= V_DEBUG) {
998 printf(_(" Memory needed: %s MiB\n"), uint64_to_str(
999 round_up_to_mib(totals.memusage_max), 0));
1000 printf(_(" Sizes in headers: %s\n"),
1001 totals.all_have_sizes ? _("Yes") : _("No"));
1009 print_totals_robot(void)
1011 char checks[CHECKS_STR_SIZE];
1012 get_check_names(checks, totals.checks, false);
1014 printf("totals\t%" PRIu64 "\t%" PRIu64 "\t%" PRIu64 "\t%" PRIu64
1015 "\t%s\t%s\t%" PRIu64 "\t%" PRIu64,
1018 totals.compressed_size,
1019 totals.uncompressed_size,
1020 get_ratio(totals.compressed_size,
1021 totals.uncompressed_size),
1023 totals.stream_padding,
1026 if (message_verbosity_get() >= V_DEBUG)
1027 printf("\t%" PRIu64 "\t%s",
1028 totals.memusage_max,
1029 totals.all_have_sizes ? "yes" : "no");
1041 // Always print totals in --robot mode. It can be convenient
1042 // in some cases and doesn't complicate usage of the
1043 // single-file case much.
1044 print_totals_robot();
1046 } else if (totals.files > 1) {
1047 // For non-robot mode, totals are printed only if there
1048 // is more than one file.
1049 if (message_verbosity_get() <= V_WARNING)
1050 print_totals_basic();
1060 list_file(const char *filename)
1062 if (opt_format != FORMAT_XZ && opt_format != FORMAT_AUTO)
1063 message_fatal(_("--list works only on .xz files "
1064 "(--format=xz or --format=auto)"));
1066 message_filename(filename);
1068 if (filename == stdin_filename) {
1069 message_error(_("--list does not support reading from "
1074 // Unset opt_stdout so that io_open_src() won't accept special files.
1075 // Set opt_force so that io_open_src() will follow symlinks.
1078 file_pair *pair = io_open_src(filename);
1082 xz_file_info xfi = XZ_FILE_INFO_INIT;
1083 if (!parse_indexes(&xfi, pair)) {
1086 // We have three main modes:
1087 // - --robot, which has submodes if --verbose is specified
1089 // - Normal --list without --verbose
1090 // - --list with one or two --verbose
1092 fail = print_info_robot(&xfi, pair);
1093 else if (message_verbosity_get() <= V_WARNING)
1094 fail = print_info_basic(&xfi, pair);
1096 fail = print_info_adv(&xfi, pair);
1098 // Update the totals that are displayed after all
1099 // the individual files have been listed. Don't count
1102 update_totals(&xfi);
1104 lzma_index_end(xfi.idx, NULL);
1107 io_close(pair, false);