1 /* $NetBSD: vnduncompress.c,v 1.11 2014/01/25 15:31:06 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.11 2014/01/25 15:31:06 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 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(MAX_BLOCKSIZE <= (SIZE_MAX / 2)); 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(MAX_N_OFFSETS <= (OFF_MAX / sizeof(uint64_t))); 158 __CTASSERT(sizeof(header) <= 159 (OFF_MAX - (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(MAX_BLOCKSIZE <= (SIZE_MAX / 2)); 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 244 static void __printflike(1,2) __dead 245 err1(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 254 static void __printflike(1,2) __dead 255 errx1(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