vnduncompress.c revision 1.2.2.3 1 1.2.2.2 tls /* $NetBSD: vnduncompress.c,v 1.2.2.3 2014/08/20 00:05:05 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.3 2014/08/20 00:05:05 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.3 tls #include <errno.h>
40 1.2.2.2 tls #include <fcntl.h>
41 1.2.2.2 tls #include <inttypes.h>
42 1.2.2.2 tls #include <limits.h>
43 1.2.2.2 tls #include <stdint.h>
44 1.2.2.2 tls #include <stdio.h>
45 1.2.2.2 tls #include <stdlib.h>
46 1.2.2.2 tls #include <unistd.h>
47 1.2.2.2 tls #include <zlib.h>
48 1.2.2.2 tls
49 1.2.2.2 tls #include "common.h"
50 1.2.2.3 tls #include "offtab.h"
51 1.2.2.3 tls #include "utils.h"
52 1.2.2.3 tls
53 1.2.2.3 tls static void err1(const char *, ...) __printflike(1,2) __dead;
54 1.2.2.3 tls static void errx1(const char *, ...) __printflike(1,2) __dead;
55 1.2.2.2 tls
56 1.2.2.2 tls int
57 1.2.2.2 tls vnduncompress(int argc, char **argv, const struct options *O __unused)
58 1.2.2.2 tls {
59 1.2.2.3 tls struct offtab offtab;
60 1.2.2.2 tls
61 1.2.2.2 tls if (argc != 2)
62 1.2.2.2 tls usage();
63 1.2.2.2 tls
64 1.2.2.2 tls const char *const cloop2_pathname = argv[0];
65 1.2.2.2 tls const char *const image_pathname = argv[1];
66 1.2.2.2 tls
67 1.2.2.2 tls /* Open the cloop2 and image files. */
68 1.2.2.2 tls const int cloop2_fd = open(cloop2_pathname, O_RDONLY);
69 1.2.2.2 tls if (cloop2_fd == -1)
70 1.2.2.2 tls err(1, "open(%s)", cloop2_pathname);
71 1.2.2.2 tls
72 1.2.2.2 tls const int image_fd = open(image_pathname,
73 1.2.2.2 tls /* XXX O_EXCL, not O_TRUNC */
74 1.2.2.2 tls (O_WRONLY | O_CREAT | O_TRUNC), 0777);
75 1.2.2.2 tls if (image_fd == -1)
76 1.2.2.2 tls err(1, "open(%s)", image_pathname);
77 1.2.2.2 tls
78 1.2.2.2 tls /* Read the header. */
79 1.2.2.2 tls struct cloop2_header header;
80 1.2.2.3 tls const ssize_t h_read = read_block(cloop2_fd, &header, sizeof(header));
81 1.2.2.2 tls if (h_read == -1)
82 1.2.2.2 tls err(1, "read header");
83 1.2.2.2 tls assert(h_read >= 0);
84 1.2.2.2 tls if ((size_t)h_read != sizeof(header))
85 1.2.2.2 tls errx(1, "partial read of header: %zu != %zu",
86 1.2.2.2 tls (size_t)h_read, sizeof(header));
87 1.2.2.2 tls
88 1.2.2.2 tls const uint32_t blocksize = be32toh(header.cl2h_blocksize);
89 1.2.2.2 tls const uint32_t n_blocks = be32toh(header.cl2h_n_blocks);
90 1.2.2.2 tls
91 1.2.2.2 tls /* Sanity-check the header parameters. */
92 1.2.2.2 tls __CTASSERT(MIN_BLOCKSIZE <= UINT32_MAX);
93 1.2.2.2 tls if (blocksize < MIN_BLOCKSIZE)
94 1.2.2.2 tls errx(1, "blocksize too small: %"PRIu32
95 1.2.2.2 tls " (must be at least %"PRIu32")",
96 1.2.2.2 tls blocksize, (uint32_t)MIN_BLOCKSIZE);
97 1.2.2.2 tls __CTASSERT(MAX_BLOCKSIZE <= UINT32_MAX);
98 1.2.2.2 tls if (MAX_BLOCKSIZE < blocksize)
99 1.2.2.2 tls errx(1, "blocksize too large: %"PRIu32
100 1.2.2.2 tls " (must be at most %"PRIu32")",
101 1.2.2.2 tls blocksize, (uint32_t)MAX_BLOCKSIZE);
102 1.2.2.2 tls __CTASSERT(DEV_BSIZE <= UINT32_MAX);
103 1.2.2.2 tls if ((blocksize % DEV_BSIZE) != 0)
104 1.2.2.2 tls errx(1, "bad blocksize: %"PRIu32
105 1.2.2.2 tls " (not a multiple of %"PRIu32")",
106 1.2.2.2 tls blocksize, (uint32_t)DEV_BSIZE);
107 1.2.2.2 tls __CTASSERT(MAX_N_BLOCKS <= UINT32_MAX);
108 1.2.2.2 tls if (MAX_N_BLOCKS < n_blocks)
109 1.2.2.2 tls errx(1, "too many blocks: %"PRIu32" (max %"PRIu32")",
110 1.2.2.2 tls n_blocks, (uint32_t)MAX_N_BLOCKS);
111 1.2.2.2 tls
112 1.2.2.3 tls /* Calculate the number of offsets we'll have to handle. */
113 1.2.2.2 tls __CTASSERT(MAX_N_BLOCKS <= (UINT32_MAX - 1));
114 1.2.2.2 tls __CTASSERT((MAX_N_BLOCKS + 1) == MAX_N_OFFSETS);
115 1.2.2.2 tls const uint32_t n_offsets = (n_blocks + 1);
116 1.2.2.2 tls
117 1.2.2.3 tls /* Choose a working window size. */
118 1.2.2.3 tls uint32_t window_size;
119 1.2.2.3 tls if (ISSET(O->flags, FLAG_w) && (O->window_size < n_offsets)) {
120 1.2.2.3 tls if (lseek(cloop2_fd, 0, SEEK_CUR) == -1) {
121 1.2.2.3 tls if (errno == ESPIPE)
122 1.2.2.3 tls errx(1, "window too small, nonseekable input");
123 1.2.2.3 tls else
124 1.2.2.3 tls err(1, "window too small and lseek failed");
125 1.2.2.3 tls }
126 1.2.2.3 tls window_size = O->window_size;
127 1.2.2.3 tls } else {
128 1.2.2.3 tls if (lseek(cloop2_fd, 0, SEEK_CUR) == -1) {
129 1.2.2.3 tls if (errno != ESPIPE)
130 1.2.2.3 tls warn("lseek");
131 1.2.2.3 tls window_size = 0;
132 1.2.2.3 tls } else {
133 1.2.2.3 tls window_size = DEF_WINDOW_SIZE;
134 1.2.2.3 tls }
135 1.2.2.3 tls }
136 1.2.2.2 tls
137 1.2.2.3 tls /* Initialize the offset table and start reading it in. */
138 1.2.2.3 tls offtab_init(&offtab, n_offsets, window_size, cloop2_fd,
139 1.2.2.3 tls CLOOP2_OFFSET_TABLE_OFFSET);
140 1.2.2.3 tls offtab_reset_read(&offtab, &err1, &errx1);
141 1.2.2.2 tls
142 1.2.2.2 tls /* Allocate compression buffers. */
143 1.2.2.2 tls /* XXX compression ratio bound */
144 1.2.2.2 tls __CTASSERT(MAX_BLOCKSIZE <= (SIZE_MAX / 2));
145 1.2.2.2 tls void *const compbuf = malloc(2 * (size_t)blocksize);
146 1.2.2.2 tls if (compbuf == NULL)
147 1.2.2.2 tls err(1, "malloc compressed buffer");
148 1.2.2.2 tls
149 1.2.2.2 tls __CTASSERT(MAX_BLOCKSIZE <= SIZE_MAX);
150 1.2.2.2 tls void *const uncompbuf = malloc(blocksize);
151 1.2.2.2 tls if (uncompbuf == NULL)
152 1.2.2.2 tls err(1, "malloc uncompressed buffer");
153 1.2.2.2 tls
154 1.2.2.2 tls /*
155 1.2.2.2 tls * Uncompress the blocks.
156 1.2.2.2 tls */
157 1.2.2.2 tls __CTASSERT(MAX_N_OFFSETS <= (OFF_MAX / sizeof(uint64_t)));
158 1.2.2.2 tls __CTASSERT(sizeof(header) <=
159 1.2.2.2 tls (OFF_MAX - (MAX_N_OFFSETS * sizeof(uint64_t))));
160 1.2.2.2 tls __CTASSERT(OFF_MAX <= UINT64_MAX);
161 1.2.2.3 tls uint64_t offset = (sizeof(header) +
162 1.2.2.3 tls ((uint64_t)n_offsets * sizeof(uint64_t)));
163 1.2.2.2 tls uint32_t blkno;
164 1.2.2.3 tls (void)offtab_prepare_get(&offtab, 0);
165 1.2.2.3 tls uint64_t last = offtab_get(&offtab, 0);
166 1.2.2.2 tls for (blkno = 0; blkno < n_blocks; blkno++) {
167 1.2.2.3 tls (void)offtab_prepare_get(&offtab, (blkno + 1));
168 1.2.2.3 tls
169 1.2.2.3 tls const uint64_t start = last;
170 1.2.2.3 tls const uint64_t end = offtab_get(&offtab, (blkno + 1));
171 1.2.2.2 tls
172 1.2.2.2 tls /* Sanity-check the offsets. */
173 1.2.2.2 tls if (start != offset)
174 1.2.2.3 tls errx(1, "strange offset for block %"PRIu32
175 1.2.2.3 tls ": 0x%"PRIx64,
176 1.2.2.2 tls blkno, start);
177 1.2.2.2 tls /* XXX compression ratio bound */
178 1.2.2.2 tls __CTASSERT(MAX_BLOCKSIZE <= (SIZE_MAX / 2));
179 1.2.2.2 tls if ((2 * (size_t)blocksize) <= (end - start))
180 1.2.2.3 tls errx(1, "block %"PRIu32" too large"
181 1.2.2.3 tls ": %"PRIu64" bytes from 0x%"PRIx64" to 0x%"PRIx64,
182 1.2.2.3 tls blkno, (end - start), start, end);
183 1.2.2.2 tls assert(offset <= MIN(OFF_MAX, UINT64_MAX));
184 1.2.2.2 tls if ((MIN(OFF_MAX, UINT64_MAX) - offset) < (end - start))
185 1.2.2.2 tls errx(1, "block %"PRIu32" overflows offset:"
186 1.2.2.3 tls " 0x%"PRIx64" + %"PRIu64,
187 1.2.2.2 tls blkno, offset, (end - start));
188 1.2.2.2 tls
189 1.2.2.2 tls /* Read the compressed block. */
190 1.2.2.3 tls const ssize_t n_read = read_block(cloop2_fd, compbuf,
191 1.2.2.3 tls (end - start));
192 1.2.2.2 tls if (n_read == -1)
193 1.2.2.2 tls err(1, "read block %"PRIu32, blkno);
194 1.2.2.2 tls assert(n_read >= 0);
195 1.2.2.2 tls if ((size_t)n_read != (end - start))
196 1.2.2.2 tls errx(1, "partial read of block %"PRIu32": %zu != %zu",
197 1.2.2.2 tls blkno, (size_t)n_read, (size_t)(end - start));
198 1.2.2.2 tls
199 1.2.2.2 tls /* Uncompress the block. */
200 1.2.2.2 tls const unsigned long complen = (end - start);
201 1.2.2.2 tls unsigned long uncomplen = blocksize;
202 1.2.2.2 tls const int zerror = uncompress(uncompbuf, &uncomplen, compbuf,
203 1.2.2.2 tls complen);
204 1.2.2.2 tls if (zerror != Z_OK)
205 1.2.2.2 tls errx(1, "block %"PRIu32" decompression failure (%d)"
206 1.2.2.2 tls ": %s", blkno, zerror, zError(zerror));
207 1.2.2.2 tls
208 1.2.2.2 tls /* Sanity-check the uncompressed length. */
209 1.2.2.2 tls assert(uncomplen <= blocksize);
210 1.2.2.2 tls if (((blkno + 1) < n_blocks) && (uncomplen != blocksize))
211 1.2.2.2 tls errx(1, "truncated non-final block %"PRIu32
212 1.2.2.2 tls ": %lu bytes", blkno, uncomplen);
213 1.2.2.2 tls
214 1.2.2.2 tls /* Write the uncompressed block. */
215 1.2.2.2 tls const ssize_t n_written = write(image_fd, uncompbuf,
216 1.2.2.2 tls uncomplen);
217 1.2.2.2 tls if (n_written == -1)
218 1.2.2.2 tls err(1, "write block %"PRIu32, blkno);
219 1.2.2.2 tls assert(n_written >= 0);
220 1.2.2.2 tls if ((size_t)n_written != uncomplen)
221 1.2.2.2 tls errx(1, "partial write of block %"PRIu32": %zu != %lu",
222 1.2.2.2 tls blkno, (size_t)n_written, uncomplen);
223 1.2.2.2 tls
224 1.2.2.2 tls /* Advance our position. */
225 1.2.2.2 tls assert((size_t)n_read <= (MIN(OFF_MAX, UINT64_MAX) - offset));
226 1.2.2.2 tls offset += (size_t)n_read;
227 1.2.2.3 tls last = end;
228 1.2.2.2 tls }
229 1.2.2.2 tls
230 1.2.2.3 tls /* Destroy the offset table and free the compression buffers. */
231 1.2.2.3 tls offtab_destroy(&offtab);
232 1.2.2.2 tls free(uncompbuf);
233 1.2.2.2 tls free(compbuf);
234 1.2.2.2 tls
235 1.2.2.2 tls /* Close the files. */
236 1.2.2.2 tls if (close(image_fd) == -1)
237 1.2.2.2 tls warn("close(image fd)");
238 1.2.2.2 tls if (close(cloop2_fd) == -1)
239 1.2.2.2 tls warn("close(cloop2 fd)");
240 1.2.2.2 tls
241 1.2.2.2 tls return 0;
242 1.2.2.2 tls }
243 1.2.2.3 tls
244 1.2.2.3 tls static void __printflike(1,2) __dead
245 1.2.2.3 tls err1(const char *fmt, ...)
246 1.2.2.3 tls {
247 1.2.2.3 tls va_list va;
248 1.2.2.3 tls
249 1.2.2.3 tls va_start(va, fmt);
250 1.2.2.3 tls verr(1, fmt, va);
251 1.2.2.3 tls va_end(va);
252 1.2.2.3 tls }
253 1.2.2.3 tls
254 1.2.2.3 tls static void __printflike(1,2) __dead
255 1.2.2.3 tls errx1(const char *fmt, ...)
256 1.2.2.3 tls {
257 1.2.2.3 tls va_list va;
258 1.2.2.3 tls
259 1.2.2.3 tls va_start(va, fmt);
260 1.2.2.3 tls verrx(1, fmt, va);
261 1.2.2.3 tls va_end(va);
262 1.2.2.3 tls }
263