]> wimlib.net Git - wimlib/blob - examples/decompressfile.c
Update progress functions
[wimlib] / examples / decompressfile.c
1 /*
2  * decompressfile.c
3  *
4  * An example of using wimlib's compression API to decompress a file compressed
5  * with the compressfile.c program.
6  *
7  * This program does *not* have anything to do with WIM files other than the
8  * fact that this makes use of compression formats that are used in WIM files.
9  * This is purely an example of using the compression API.
10  *
11  * Compile with:
12  *
13  *    $ gcc decompressfile.c -o decompressfile -lwim
14  *
15  * Run with:
16  *
17  *    $ ./decompressfile INFILE OUTFILE
18  *
19  * For example:
20  *
21  *    $ ./compressfile book.txt book.txt.lzms LZMS 1048576
22  *    $ rm -f book.txt
23  *    $ ./decompressfile book.txt.lzms book.txt
24  *
25  * The compressed file format created here is simply a series of compressed
26  * chunks.  A real format would need to have checksums and other metadata.
27  */
28
29 #define _GNU_SOURCE
30 #define _FILE_OFFSET_BITS 64
31
32 #include <wimlib.h>
33
34 #include <errno.h>
35 #include <error.h>
36 #include <fcntl.h>
37 #include <inttypes.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41
42 static void
43 do_decompress(int in_fd, const char *in_filename,
44               int out_fd, const char *out_filename,
45               uint32_t chunk_size, struct wimlib_decompressor *decompressor)
46 {
47         uint64_t chunk_num;
48
49         char *ubuf = malloc(chunk_size);
50         char *cbuf = malloc(chunk_size - 1);
51
52         for (chunk_num = 1; ; chunk_num++) {
53                 ssize_t bytes_read;
54                 uint32_t usize;
55                 uint32_t csize;
56
57                 /* Read chunk uncompressed and compressed sizes.  */
58                 bytes_read = read(in_fd, &usize, sizeof(uint32_t));
59                 if (bytes_read == 0)
60                         break;
61
62                 if (bytes_read != sizeof(uint32_t) ||
63                     read(in_fd, &csize, sizeof(uint32_t)) != sizeof(uint32_t))
64                 {
65                         error(1, errno, "Error reading \"%s\"", in_filename);
66                 }
67
68                 if (csize > usize || usize > chunk_size)
69                         error(1, 0, "The data is invalid!");
70
71                 if (usize == csize) {
72                         if (read(in_fd, ubuf, usize) != usize) {
73                                 error(1, errno, "Error reading \"%s\"",
74                                       in_filename);
75                         }
76                 } else {
77                         if (read(in_fd, cbuf, csize) != csize) {
78                                 error(1, errno, "Error reading \"%s\"",
79                                       in_filename);
80                         }
81
82                         if (wimlib_decompress(cbuf, csize, ubuf, usize,
83                                               decompressor))
84                         {
85                                 error(1, 0, "The compressed data is invalid!");
86                         }
87                 }
88
89                 printf("Chunk %"PRIu64": %"PRIu32" => %"PRIu32" bytes\n",
90                        chunk_num, csize, usize);
91
92                 /* Output the uncompressed chunk size, the compressed chunk
93                  * size, then the chunk data.  Note: a real program would need
94                  * to output the chunk sizes in consistent endianness.  */
95                 if (write(out_fd, ubuf, usize) != usize)
96                         error(1, errno, "Error writing to \"%s\"", out_filename);
97         }
98         free(ubuf);
99         free(cbuf);
100 }
101
102 int main(int argc, char **argv)
103 {
104         const char *in_filename;
105         const char *out_filename;
106         int in_fd;
107         int out_fd;
108         uint32_t ctype;
109         uint32_t chunk_size;
110         int ret;
111         struct wimlib_decompressor *decompressor;
112
113         if (argc != 3) {
114                 fprintf(stderr, "Usage: %s INFILE OUTFILE\n", argv[0]);
115                 return 2;
116         }
117
118         in_filename = argv[1];
119         out_filename = argv[2];
120
121         /* Open input file and output file.  */
122         in_fd = open(in_filename, O_RDONLY);
123         if (in_fd < 0)
124                 error(1, errno, "Failed to open \"%s\"", in_filename);
125         out_fd = open(out_filename, O_WRONLY | O_TRUNC | O_CREAT, 0644);
126         if (out_fd < 0)
127                 error(1, errno, "Failed to open \"%s\"", out_filename);
128
129         /* Get compression type and chunk size.  */
130         if (read(in_fd, &ctype, sizeof(uint32_t)) != sizeof(uint32_t) ||
131             read(in_fd, &chunk_size, sizeof(uint32_t)) != sizeof(uint32_t))
132                 error(1, errno, "Error reading from \"%s\"", in_filename);
133
134         /* Create a decompressor for the compression type and chunk size with
135          * the default parameters.  */
136         ret = wimlib_create_decompressor(ctype, chunk_size, NULL, &decompressor);
137         if (ret != 0)
138                 error(1, 0, "Failed to create decompressor: %s",
139                       wimlib_get_error_string(ret));
140
141         /* Decompress and write the data.  */
142         do_decompress(in_fd, in_filename,
143                       out_fd, out_filename,
144                       chunk_size, decompressor);
145
146         /* Cleanup and return.  */
147         if (close(out_fd))
148                 error(1, errno, "Error closing \"%s\"", out_filename);
149         wimlib_free_decompressor(decompressor);
150         return 0;
151 }