4 * An example of using wimlib's compression API to decompress a file compressed
5 * with the compressfile.c program.
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.
13 * $ gcc decompressfile.c -o decompressfile -lwim
17 * $ ./decompressfile INFILE OUTFILE
21 * $ ./compressfile book.txt book.txt.lzms LZMS 1048576
23 * $ ./decompressfile book.txt.lzms book.txt
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.
30 #define _FILE_OFFSET_BITS 64
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)
49 char *ubuf = malloc(chunk_size);
50 char *cbuf = malloc(chunk_size - 1);
52 for (chunk_num = 1; ; chunk_num++) {
57 /* Read chunk uncompressed and compressed sizes. */
58 bytes_read = read(in_fd, &usize, sizeof(uint32_t));
62 if (bytes_read != sizeof(uint32_t) ||
63 read(in_fd, &csize, sizeof(uint32_t)) != sizeof(uint32_t))
65 error(1, errno, "Error reading \"%s\"", in_filename);
68 if (csize > usize || usize > chunk_size)
69 error(1, 0, "The data is invalid!");
72 if (read(in_fd, ubuf, usize) != usize) {
73 error(1, errno, "Error reading \"%s\"",
77 if (read(in_fd, cbuf, csize) != csize) {
78 error(1, errno, "Error reading \"%s\"",
82 if (wimlib_decompress(cbuf, csize, ubuf, usize,
85 error(1, 0, "The compressed data is invalid!");
89 printf("Chunk %"PRIu64": %"PRIu32" => %"PRIu32" bytes\n",
90 chunk_num, csize, usize);
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);
102 int main(int argc, char **argv)
104 const char *in_filename;
105 const char *out_filename;
111 struct wimlib_decompressor *decompressor;
114 fprintf(stderr, "Usage: %s INFILE OUTFILE\n", argv[0]);
118 in_filename = argv[1];
119 out_filename = argv[2];
121 /* Open input file and output file. */
122 in_fd = open(in_filename, O_RDONLY);
124 error(1, errno, "Failed to open \"%s\"", in_filename);
125 out_fd = open(out_filename, O_WRONLY | O_TRUNC | O_CREAT, 0644);
127 error(1, errno, "Failed to open \"%s\"", out_filename);
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);
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);
138 error(1, 0, "Failed to create decompressor: %s",
139 wimlib_get_error_string(ret));
141 /* Decompress and write the data. */
142 do_decompress(in_fd, in_filename,
143 out_fd, out_filename,
144 chunk_size, decompressor);
146 /* Cleanup and return. */
148 error(1, errno, "Error closing \"%s\"", out_filename);
149 wimlib_free_decompressor(decompressor);