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