1 // SPDX-License-Identifier: 0BSD
3 ///////////////////////////////////////////////////////////////////////////////
5 /// \file 11_file_info.c
6 /// \brief Get uncompressed size of .xz file(s)
8 /// Usage: ./11_file_info INFILE1.xz [INFILEn.xz]...
10 /// Example: ./11_file_info foo.xz
12 // Author: Lasse Collin
14 ///////////////////////////////////////////////////////////////////////////////
26 print_file_size(lzma_stream
*strm
, FILE *infile
, const char *filename
)
28 // Get the file size. In standard C it can be done by seeking to
29 // the end of the file and then getting the file position.
30 // In POSIX one can use fstat() and then st_size from struct stat.
31 // Also note that fseek() and ftell() use long and thus don't support
32 // large files on 32-bit systems (POSIX versions fseeko() and
33 // ftello() can support large files).
34 if (fseek(infile
, 0, SEEK_END
)) {
35 fprintf(stderr
, "Error seeking the file '%s': %s\n",
36 filename
, strerror(errno
));
40 const long file_size
= ftell(infile
);
42 // The decoder wants to start from the beginning of the .xz file.
45 // Initialize the decoder.
47 lzma_ret ret
= lzma_file_info_decoder(strm
, &i
, UINT64_MAX
,
51 // Initialization succeeded.
55 fprintf(stderr
, "Out of memory when initializing "
56 "the .xz file info decoder\n");
61 fprintf(stderr
, "Unknown error, possibly a bug\n");
65 // This example program reuses the same lzma_stream structure
66 // for multiple files, so we need to reset this when starting
70 // Buffer for input data.
71 uint8_t inbuf
[BUFSIZ
];
73 // Pass data to the decoder and seek when needed.
75 if (strm
->avail_in
== 0) {
76 strm
->next_in
= inbuf
;
77 strm
->avail_in
= fread(inbuf
, 1, sizeof(inbuf
),
82 "Error reading from '%s': %s\n",
83 filename
, strerror(errno
));
87 // We don't need to care about hitting the end of
88 // the file so no need to check for feof().
91 ret
= lzma_code(strm
, LZMA_RUN
);
97 case LZMA_SEEK_NEEDED
:
98 // The cast is safe because liblzma won't ask us to
99 // seek past the known size of the input file which
100 // did fit into a long.
102 // NOTE: Remember to change these to off_t if you
103 // switch fseeko() or lseek().
104 if (fseek(infile
, (long)(strm
->seek_pos
), SEEK_SET
)) {
105 fprintf(stderr
, "Error seeking the "
107 filename
, strerror(errno
));
111 // The old data in the inbuf is useless now. Set
112 // avail_in to zero so that we will read new input
113 // from the new file position on the next iteration
118 case LZMA_STREAM_END
:
119 // File information was successfully decoded.
120 // See <lzma/index.h> for functions that can be
121 // used on it. In this example we just print
122 // the uncompressed size (in bytes) of
123 // the .xz file followed by its file name.
124 printf("%10" PRIu64
" %s\n",
125 lzma_index_uncompressed_size(i
),
128 // Free the memory of the lzma_index structure.
129 lzma_index_end(i
, NULL
);
133 case LZMA_FORMAT_ERROR
:
134 // .xz magic bytes weren't found.
135 fprintf(stderr
, "The file '%s' is not "
136 "in the .xz format\n", filename
);
139 case LZMA_OPTIONS_ERROR
:
140 fprintf(stderr
, "The file '%s' has .xz headers that "
141 "are not supported by this liblzma "
142 "version\n", filename
);
145 case LZMA_DATA_ERROR
:
146 fprintf(stderr
, "The file '%s' is corrupt\n",
151 fprintf(stderr
, "Memory allocation failed when "
152 "decoding the file '%s'\n", filename
);
155 // LZMA_MEMLIMIT_ERROR shouldn't happen because we used
156 // UINT64_MAX as the limit.
158 // LZMA_BUF_ERROR shouldn't happen because we always provide
159 // new input when the input buffer is empty. The decoder
160 // knows the input file size and thus won't try to read past
161 // the end of the file.
162 case LZMA_MEMLIMIT_ERROR
:
164 case LZMA_PROG_ERROR
:
166 fprintf(stderr
, "Unknown error, possibly a bug\n");
171 // This line is never reached.
176 main(int argc
, char **argv
)
179 lzma_stream strm
= LZMA_STREAM_INIT
;
181 for (int i
= 1; i
< argc
; ++i
) {
182 FILE *infile
= fopen(argv
[i
], "rb");
184 if (infile
== NULL
) {
185 fprintf(stderr
, "Cannot open the file '%s': %s\n",
186 argv
[i
], strerror(errno
));
190 success
&= print_file_size(&strm
, infile
, argv
[i
]);
192 (void)fclose(infile
);
197 // Close stdout to catch possible write errors that can occur
198 // when pending data is flushed from the stdio buffers.
199 if (fclose(stdout
)) {
200 fprintf(stderr
, "Write error: %s\n", strerror(errno
));
204 return success
? EXIT_SUCCESS
: EXIT_FAILURE
;