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