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