Translations: Update the Chinese (traditional) translation.
[xz/debian.git] / doc / examples / 11_file_info.c
blob9e7b0c8e62bf3e3942d300076c5fa9033c2fb165
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file 11_file_info.c
4 /// \brief Get uncompressed size of .xz file(s)
5 ///
6 /// Usage: ./11_file_info INFILE1.xz [INFILEn.xz]...
7 ///
8 /// Example: ./11_file_info foo.xz
9 //
10 // Author: Lasse Collin
12 // This file has been put into the public domain.
13 // You can do whatever you want with this file.
15 ///////////////////////////////////////////////////////////////////////////////
17 #include <stdbool.h>
18 #include <inttypes.h>
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <lzma.h>
26 static bool
27 print_file_size(lzma_stream *strm, FILE *infile, const char *filename)
29 // Get the file size. In standard C it can be done by seeking to
30 // the end of the file and then getting the file position.
31 // In POSIX one can use fstat() and then st_size from struct stat.
32 // Also note that fseek() and ftell() use long and thus don't support
33 // large files on 32-bit systems (POSIX versions fseeko() and
34 // ftello() can support large files).
35 if (fseek(infile, 0, SEEK_END)) {
36 fprintf(stderr, "Error seeking the file `%s': %s\n",
37 filename, strerror(errno));
38 return false;
41 const long file_size = ftell(infile);
43 // The decoder wants to start from the beginning of the .xz file.
44 rewind(infile);
46 // Initialize the decoder.
47 lzma_index *i;
48 lzma_ret ret = lzma_file_info_decoder(strm, &i, UINT64_MAX,
49 (uint64_t)file_size);
50 switch (ret) {
51 case LZMA_OK:
52 // Initialization succeeded.
53 break;
55 case LZMA_MEM_ERROR:
56 fprintf(stderr, "Out of memory when initializing "
57 "the .xz file info decoder\n");
58 return false;
60 case LZMA_PROG_ERROR:
61 default:
62 fprintf(stderr, "Unknown error, possibly a bug\n");
63 return false;
66 // This example program reuses the same lzma_stream structure
67 // for multiple files, so we need to reset this when starting
68 // a new file.
69 strm->avail_in = 0;
71 // Buffer for input data.
72 uint8_t inbuf[BUFSIZ];
74 // Pass data to the decoder and seek when needed.
75 while (true) {
76 if (strm->avail_in == 0) {
77 strm->next_in = inbuf;
78 strm->avail_in = fread(inbuf, 1, sizeof(inbuf),
79 infile);
81 if (ferror(infile)) {
82 fprintf(stderr,
83 "Error reading from `%s': %s\n",
84 filename, strerror(errno));
85 return false;
88 // We don't need to care about hitting the end of
89 // the file so no need to check for feof().
92 ret = lzma_code(strm, LZMA_RUN);
94 switch (ret) {
95 case LZMA_OK:
96 break;
98 case LZMA_SEEK_NEEDED:
99 // The cast is safe because liblzma won't ask us to
100 // seek past the known size of the input file which
101 // did fit into a long.
103 // NOTE: Remember to change these to off_t if you
104 // switch fseeko() or lseek().
105 if (fseek(infile, (long)(strm->seek_pos), SEEK_SET)) {
106 fprintf(stderr, "Error seeking the "
107 "file `%s': %s\n",
108 filename, strerror(errno));
109 return false;
112 // The old data in the inbuf is useless now. Set
113 // avail_in to zero so that we will read new input
114 // from the new file position on the next iteration
115 // of this loop.
116 strm->avail_in = 0;
117 break;
119 case LZMA_STREAM_END:
120 // File information was successfully decoded.
121 // See <lzma/index.h> for functions that can be
122 // used on it. In this example we just print
123 // the uncompressed size (in bytes) of
124 // the .xz file followed by its file name.
125 printf("%10" PRIu64 " %s\n",
126 lzma_index_uncompressed_size(i),
127 filename);
129 // Free the memory of the lzma_index structure.
130 lzma_index_end(i, NULL);
132 return true;
134 case LZMA_FORMAT_ERROR:
135 // .xz magic bytes weren't found.
136 fprintf(stderr, "The file `%s' is not "
137 "in the .xz format\n", filename);
138 return false;
140 case LZMA_OPTIONS_ERROR:
141 fprintf(stderr, "The file `%s' has .xz headers that "
142 "are not supported by this liblzma "
143 "version\n", filename);
144 return false;
146 case LZMA_DATA_ERROR:
147 fprintf(stderr, "The file `%s' is corrupt\n",
148 filename);
149 return false;
151 case LZMA_MEM_ERROR:
152 fprintf(stderr, "Memory allocation failed when "
153 "decoding the file `%s'\n", filename);
154 return false;
156 // LZMA_MEMLIMIT_ERROR shouldn't happen because we used
157 // UINT64_MAX as the limit.
159 // LZMA_BUF_ERROR shouldn't happen because we always provide
160 // new input when the input buffer is empty. The decoder
161 // knows the input file size and thus won't try to read past
162 // the end of the file.
163 case LZMA_MEMLIMIT_ERROR:
164 case LZMA_BUF_ERROR:
165 case LZMA_PROG_ERROR:
166 default:
167 fprintf(stderr, "Unknown error, possibly a bug\n");
168 return false;
172 // This line is never reached.
176 extern int
177 main(int argc, char **argv)
179 bool success = true;
180 lzma_stream strm = LZMA_STREAM_INIT;
182 for (int i = 1; i < argc; ++i) {
183 FILE *infile = fopen(argv[i], "rb");
185 if (infile == NULL) {
186 fprintf(stderr, "Cannot open the file `%s': %s\n",
187 argv[i], strerror(errno));
188 success = false;
191 success &= print_file_size(&strm, infile, argv[i]);
193 (void)fclose(infile);
196 lzma_end(&strm);
198 // Close stdout to catch possible write errors that can occur
199 // when pending data is flushed from the stdio buffers.
200 if (fclose(stdout)) {
201 fprintf(stderr, "Write error: %s\n", strerror(errno));
202 success = false;
205 return success ? EXIT_SUCCESS : EXIT_FAILURE;