vnduncompress.c revision 1.2.2.2 1 1.2.2.2 tls /* $NetBSD: vnduncompress.c,v 1.2.2.2 2013/06/23 06:29:02 tls Exp $ */
2 1.2.2.2 tls
3 1.2.2.2 tls /*-
4 1.2.2.2 tls * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.2.2.2 tls * All rights reserved.
6 1.2.2.2 tls *
7 1.2.2.2 tls * This code is derived from software contributed to The NetBSD Foundation
8 1.2.2.2 tls * by Taylor R. Campbell.
9 1.2.2.2 tls *
10 1.2.2.2 tls * Redistribution and use in source and binary forms, with or without
11 1.2.2.2 tls * modification, are permitted provided that the following conditions
12 1.2.2.2 tls * are met:
13 1.2.2.2 tls * 1. Redistributions of source code must retain the above copyright
14 1.2.2.2 tls * notice, this list of conditions and the following disclaimer.
15 1.2.2.2 tls * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.2.2 tls * notice, this list of conditions and the following disclaimer in the
17 1.2.2.2 tls * documentation and/or other materials provided with the distribution.
18 1.2.2.2 tls *
19 1.2.2.2 tls * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2.2.2 tls * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2.2.2 tls * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2.2.2 tls * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2.2.2 tls * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2.2.2 tls * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2.2.2 tls * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2.2.2 tls * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2.2.2 tls * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2.2.2 tls * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2.2.2 tls * POSSIBILITY OF SUCH DAMAGE.
30 1.2.2.2 tls */
31 1.2.2.2 tls
32 1.2.2.2 tls #include <sys/cdefs.h>
33 1.2.2.2 tls __RCSID("$NetBSD: vnduncompress.c,v 1.2.2.2 2013/06/23 06:29:02 tls Exp $");
34 1.2.2.2 tls
35 1.2.2.2 tls #include <sys/endian.h>
36 1.2.2.2 tls
37 1.2.2.2 tls #include <assert.h>
38 1.2.2.2 tls #include <err.h>
39 1.2.2.2 tls #include <fcntl.h>
40 1.2.2.2 tls #include <inttypes.h>
41 1.2.2.2 tls #include <limits.h>
42 1.2.2.2 tls #include <stdint.h>
43 1.2.2.2 tls #include <stdio.h>
44 1.2.2.2 tls #include <stdlib.h>
45 1.2.2.2 tls #include <unistd.h>
46 1.2.2.2 tls #include <zlib.h>
47 1.2.2.2 tls
48 1.2.2.2 tls #include "common.h"
49 1.2.2.2 tls
50 1.2.2.2 tls int
51 1.2.2.2 tls vnduncompress(int argc, char **argv, const struct options *O __unused)
52 1.2.2.2 tls {
53 1.2.2.2 tls
54 1.2.2.2 tls if (argc != 2)
55 1.2.2.2 tls usage();
56 1.2.2.2 tls
57 1.2.2.2 tls const char *const cloop2_pathname = argv[0];
58 1.2.2.2 tls const char *const image_pathname = argv[1];
59 1.2.2.2 tls
60 1.2.2.2 tls /* Open the cloop2 and image files. */
61 1.2.2.2 tls const int cloop2_fd = open(cloop2_pathname, O_RDONLY);
62 1.2.2.2 tls if (cloop2_fd == -1)
63 1.2.2.2 tls err(1, "open(%s)", cloop2_pathname);
64 1.2.2.2 tls
65 1.2.2.2 tls const int image_fd = open(image_pathname,
66 1.2.2.2 tls /* XXX O_EXCL, not O_TRUNC */
67 1.2.2.2 tls (O_WRONLY | O_CREAT | O_TRUNC), 0777);
68 1.2.2.2 tls if (image_fd == -1)
69 1.2.2.2 tls err(1, "open(%s)", image_pathname);
70 1.2.2.2 tls
71 1.2.2.2 tls /* Read the header. */
72 1.2.2.2 tls struct cloop2_header header;
73 1.2.2.2 tls const ssize_t h_read = read(cloop2_fd, &header, sizeof(header));
74 1.2.2.2 tls if (h_read == -1)
75 1.2.2.2 tls err(1, "read header");
76 1.2.2.2 tls assert(h_read >= 0);
77 1.2.2.2 tls if ((size_t)h_read != sizeof(header))
78 1.2.2.2 tls errx(1, "partial read of header: %zu != %zu",
79 1.2.2.2 tls (size_t)h_read, sizeof(header));
80 1.2.2.2 tls
81 1.2.2.2 tls const uint32_t blocksize = be32toh(header.cl2h_blocksize);
82 1.2.2.2 tls const uint32_t n_blocks = be32toh(header.cl2h_n_blocks);
83 1.2.2.2 tls
84 1.2.2.2 tls /* Sanity-check the header parameters. */
85 1.2.2.2 tls __CTASSERT(MIN_BLOCKSIZE <= UINT32_MAX);
86 1.2.2.2 tls if (blocksize < MIN_BLOCKSIZE)
87 1.2.2.2 tls errx(1, "blocksize too small: %"PRIu32
88 1.2.2.2 tls " (must be at least %"PRIu32")",
89 1.2.2.2 tls blocksize, (uint32_t)MIN_BLOCKSIZE);
90 1.2.2.2 tls __CTASSERT(MAX_BLOCKSIZE <= UINT32_MAX);
91 1.2.2.2 tls if (MAX_BLOCKSIZE < blocksize)
92 1.2.2.2 tls errx(1, "blocksize too large: %"PRIu32
93 1.2.2.2 tls " (must be at most %"PRIu32")",
94 1.2.2.2 tls blocksize, (uint32_t)MAX_BLOCKSIZE);
95 1.2.2.2 tls __CTASSERT(DEV_BSIZE <= UINT32_MAX);
96 1.2.2.2 tls if ((blocksize % DEV_BSIZE) != 0)
97 1.2.2.2 tls errx(1, "bad blocksize: %"PRIu32
98 1.2.2.2 tls " (not a multiple of %"PRIu32")",
99 1.2.2.2 tls blocksize, (uint32_t)DEV_BSIZE);
100 1.2.2.2 tls __CTASSERT(MAX_N_BLOCKS <= UINT32_MAX);
101 1.2.2.2 tls if (MAX_N_BLOCKS < n_blocks)
102 1.2.2.2 tls errx(1, "too many blocks: %"PRIu32" (max %"PRIu32")",
103 1.2.2.2 tls n_blocks, (uint32_t)MAX_N_BLOCKS);
104 1.2.2.2 tls
105 1.2.2.2 tls /* Allocate an offset table. */
106 1.2.2.2 tls __CTASSERT(MAX_N_BLOCKS <= (UINT32_MAX - 1));
107 1.2.2.2 tls __CTASSERT((MAX_N_BLOCKS + 1) == MAX_N_OFFSETS);
108 1.2.2.2 tls const uint32_t n_offsets = (n_blocks + 1);
109 1.2.2.2 tls
110 1.2.2.2 tls __CTASSERT(MAX_N_OFFSETS <= (SIZE_MAX / sizeof(uint64_t)));
111 1.2.2.2 tls uint64_t *const offset_table = malloc(n_offsets * sizeof(uint64_t));
112 1.2.2.2 tls
113 1.2.2.2 tls /* Read the offset table in. */
114 1.2.2.2 tls const ssize_t ot_read = read(cloop2_fd, offset_table,
115 1.2.2.2 tls (n_offsets * sizeof(uint64_t)));
116 1.2.2.2 tls if (ot_read == -1)
117 1.2.2.2 tls err(1, "read offset table");
118 1.2.2.2 tls assert(ot_read >= 0);
119 1.2.2.2 tls if ((size_t)ot_read != (n_offsets * sizeof(uint64_t)))
120 1.2.2.2 tls errx(1, "partial read of offset table: %zu != %zu",
121 1.2.2.2 tls (size_t)ot_read, (size_t)(n_offsets * sizeof(uint64_t)));
122 1.2.2.2 tls
123 1.2.2.2 tls /* Allocate compression buffers. */
124 1.2.2.2 tls /* XXX compression ratio bound */
125 1.2.2.2 tls __CTASSERT(MAX_BLOCKSIZE <= (SIZE_MAX / 2));
126 1.2.2.2 tls void *const compbuf = malloc(2 * (size_t)blocksize);
127 1.2.2.2 tls if (compbuf == NULL)
128 1.2.2.2 tls err(1, "malloc compressed buffer");
129 1.2.2.2 tls
130 1.2.2.2 tls __CTASSERT(MAX_BLOCKSIZE <= SIZE_MAX);
131 1.2.2.2 tls void *const uncompbuf = malloc(blocksize);
132 1.2.2.2 tls if (uncompbuf == NULL)
133 1.2.2.2 tls err(1, "malloc uncompressed buffer");
134 1.2.2.2 tls
135 1.2.2.2 tls /*
136 1.2.2.2 tls * Uncompress the blocks.
137 1.2.2.2 tls */
138 1.2.2.2 tls __CTASSERT(MAX_N_OFFSETS <= (OFF_MAX / sizeof(uint64_t)));
139 1.2.2.2 tls __CTASSERT(sizeof(header) <=
140 1.2.2.2 tls (OFF_MAX - (MAX_N_OFFSETS * sizeof(uint64_t))));
141 1.2.2.2 tls __CTASSERT(OFF_MAX <= UINT64_MAX);
142 1.2.2.2 tls uint64_t offset = (sizeof(header) + (n_offsets * sizeof(uint64_t)));
143 1.2.2.2 tls uint32_t blkno;
144 1.2.2.2 tls for (blkno = 0; blkno < n_blocks; blkno++) {
145 1.2.2.2 tls const uint64_t start = be64toh(offset_table[blkno]);
146 1.2.2.2 tls const uint64_t end = be64toh(offset_table[blkno + 1]);
147 1.2.2.2 tls
148 1.2.2.2 tls /* Sanity-check the offsets. */
149 1.2.2.2 tls if (start != offset)
150 1.2.2.2 tls errx(1, "strange offset for block %"PRIu32": %"PRIu64,
151 1.2.2.2 tls blkno, start);
152 1.2.2.2 tls /* XXX compression ratio bound */
153 1.2.2.2 tls __CTASSERT(MAX_BLOCKSIZE <= (SIZE_MAX / 2));
154 1.2.2.2 tls if ((2 * (size_t)blocksize) <= (end - start))
155 1.2.2.2 tls errx(1, "block %"PRIu32" too large: %"PRIu64" bytes",
156 1.2.2.2 tls blkno, (end - start));
157 1.2.2.2 tls assert(offset <= MIN(OFF_MAX, UINT64_MAX));
158 1.2.2.2 tls if ((MIN(OFF_MAX, UINT64_MAX) - offset) < (end - start))
159 1.2.2.2 tls errx(1, "block %"PRIu32" overflows offset:"
160 1.2.2.2 tls " %"PRIu64" + %"PRIu64,
161 1.2.2.2 tls blkno, offset, (end - start));
162 1.2.2.2 tls
163 1.2.2.2 tls /* Read the compressed block. */
164 1.2.2.2 tls const ssize_t n_read = read(cloop2_fd, compbuf, (end - start));
165 1.2.2.2 tls if (n_read == -1)
166 1.2.2.2 tls err(1, "read block %"PRIu32, blkno);
167 1.2.2.2 tls assert(n_read >= 0);
168 1.2.2.2 tls if ((size_t)n_read != (end - start))
169 1.2.2.2 tls errx(1, "partial read of block %"PRIu32": %zu != %zu",
170 1.2.2.2 tls blkno, (size_t)n_read, (size_t)(end - start));
171 1.2.2.2 tls
172 1.2.2.2 tls /* Uncompress the block. */
173 1.2.2.2 tls const unsigned long complen = (end - start);
174 1.2.2.2 tls unsigned long uncomplen = blocksize;
175 1.2.2.2 tls const int zerror = uncompress(uncompbuf, &uncomplen, compbuf,
176 1.2.2.2 tls complen);
177 1.2.2.2 tls if (zerror != Z_OK)
178 1.2.2.2 tls errx(1, "block %"PRIu32" decompression failure (%d)"
179 1.2.2.2 tls ": %s", blkno, zerror, zError(zerror));
180 1.2.2.2 tls
181 1.2.2.2 tls /* Sanity-check the uncompressed length. */
182 1.2.2.2 tls assert(uncomplen <= blocksize);
183 1.2.2.2 tls if (((blkno + 1) < n_blocks) && (uncomplen != blocksize))
184 1.2.2.2 tls errx(1, "truncated non-final block %"PRIu32
185 1.2.2.2 tls ": %lu bytes", blkno, uncomplen);
186 1.2.2.2 tls
187 1.2.2.2 tls /* Write the uncompressed block. */
188 1.2.2.2 tls const ssize_t n_written = write(image_fd, uncompbuf,
189 1.2.2.2 tls uncomplen);
190 1.2.2.2 tls if (n_written == -1)
191 1.2.2.2 tls err(1, "write block %"PRIu32, blkno);
192 1.2.2.2 tls assert(n_written >= 0);
193 1.2.2.2 tls if ((size_t)n_written != uncomplen)
194 1.2.2.2 tls errx(1, "partial write of block %"PRIu32": %zu != %lu",
195 1.2.2.2 tls blkno, (size_t)n_written, uncomplen);
196 1.2.2.2 tls
197 1.2.2.2 tls /* Advance our position. */
198 1.2.2.2 tls assert((size_t)n_read <= (MIN(OFF_MAX, UINT64_MAX) - offset));
199 1.2.2.2 tls offset += (size_t)n_read;
200 1.2.2.2 tls }
201 1.2.2.2 tls
202 1.2.2.2 tls /* Free the offset table and compression buffers. */
203 1.2.2.2 tls free(offset_table);
204 1.2.2.2 tls free(uncompbuf);
205 1.2.2.2 tls free(compbuf);
206 1.2.2.2 tls
207 1.2.2.2 tls /* Close the files. */
208 1.2.2.2 tls if (close(image_fd) == -1)
209 1.2.2.2 tls warn("close(image fd)");
210 1.2.2.2 tls if (close(cloop2_fd) == -1)
211 1.2.2.2 tls warn("close(cloop2 fd)");
212 1.2.2.2 tls
213 1.2.2.2 tls return 0;
214 1.2.2.2 tls }
215