vnduncompress.c revision 1.1 1 1.1 riastrad /* $NetBSD: vnduncompress.c,v 1.1 2013/05/03 23:28:15 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.1 riastrad __RCSID("$NetBSD: vnduncompress.c,v 1.1 2013/05/03 23:28:15 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.1 riastrad #include <fcntl.h>
40 1.1 riastrad #include <inttypes.h>
41 1.1 riastrad #include <limits.h>
42 1.1 riastrad #include <stdint.h>
43 1.1 riastrad #include <stdio.h>
44 1.1 riastrad #include <stdlib.h>
45 1.1 riastrad #include <unistd.h>
46 1.1 riastrad #include <zlib.h>
47 1.1 riastrad
48 1.1 riastrad #include "common.h"
49 1.1 riastrad
50 1.1 riastrad int
51 1.1 riastrad vnduncompress(int argc, char **argv, const struct options *O __unused)
52 1.1 riastrad {
53 1.1 riastrad
54 1.1 riastrad if (argc != 2)
55 1.1 riastrad usage();
56 1.1 riastrad
57 1.1 riastrad const char *const cloop2_pathname = argv[0];
58 1.1 riastrad const char *const image_pathname = argv[1];
59 1.1 riastrad
60 1.1 riastrad /* Open the cloop2 and image files. */
61 1.1 riastrad const int cloop2_fd = open(cloop2_pathname, O_RDONLY);
62 1.1 riastrad if (cloop2_fd == -1)
63 1.1 riastrad err(1, "open(%s)", cloop2_pathname);
64 1.1 riastrad
65 1.1 riastrad const int image_fd = open(image_pathname,
66 1.1 riastrad /* XXX O_EXCL, not O_TRUNC */
67 1.1 riastrad (O_WRONLY | O_CREAT | O_TRUNC), 0777);
68 1.1 riastrad if (image_fd == -1)
69 1.1 riastrad err(1, "open(%s)", image_pathname);
70 1.1 riastrad
71 1.1 riastrad /* Read the header. */
72 1.1 riastrad struct cloop2_header header;
73 1.1 riastrad const ssize_t h_read = read(cloop2_fd, &header, sizeof(header));
74 1.1 riastrad if (h_read == -1)
75 1.1 riastrad err(1, "read header");
76 1.1 riastrad assert(h_read >= 0);
77 1.1 riastrad if ((size_t)h_read != sizeof(header))
78 1.1 riastrad errx(1, "partial read of header: %zu", (size_t)h_read);
79 1.1 riastrad
80 1.1 riastrad const uint32_t blocksize = be32toh(header.cl2h_blocksize);
81 1.1 riastrad const uint32_t n_blocks = be32toh(header.cl2h_n_blocks);
82 1.1 riastrad
83 1.1 riastrad /* Sanity-check the header parameters. */
84 1.1 riastrad __CTASSERT(MIN_BLOCKSIZE <= UINT32_MAX);
85 1.1 riastrad if (blocksize < MIN_BLOCKSIZE)
86 1.1 riastrad errx(1, "blocksize too small: %"PRIu32
87 1.1 riastrad " (must be at least %"PRIu32")",
88 1.1 riastrad blocksize, (uint32_t)MIN_BLOCKSIZE);
89 1.1 riastrad __CTASSERT(MAX_BLOCKSIZE <= UINT32_MAX);
90 1.1 riastrad if (MAX_BLOCKSIZE < blocksize)
91 1.1 riastrad errx(1, "blocksize too large: %"PRIu32
92 1.1 riastrad " (must be at most %"PRIu32")",
93 1.1 riastrad blocksize, (uint32_t)MAX_BLOCKSIZE);
94 1.1 riastrad __CTASSERT(DEV_BSIZE <= UINT32_MAX);
95 1.1 riastrad if ((blocksize % DEV_BSIZE) != 0)
96 1.1 riastrad errx(1, "bad blocksize: %"PRIu32
97 1.1 riastrad " (not a multiple of %"PRIu32")",
98 1.1 riastrad blocksize, (uint32_t)DEV_BSIZE);
99 1.1 riastrad __CTASSERT(MAX_N_BLOCKS <= UINT32_MAX);
100 1.1 riastrad if (MAX_N_BLOCKS < n_blocks)
101 1.1 riastrad errx(1, "too many blocks: %"PRIu32" (max %"PRIu32")",
102 1.1 riastrad n_blocks, (uint32_t)MAX_N_BLOCKS);
103 1.1 riastrad
104 1.1 riastrad /* Allocate an offset table. */
105 1.1 riastrad __CTASSERT(MAX_N_BLOCKS <= (UINT32_MAX - 1));
106 1.1 riastrad __CTASSERT((MAX_N_BLOCKS + 1) == MAX_N_OFFSETS);
107 1.1 riastrad const uint32_t n_offsets = (n_blocks + 1);
108 1.1 riastrad
109 1.1 riastrad __CTASSERT(MAX_N_OFFSETS <= (SIZE_MAX / sizeof(uint64_t)));
110 1.1 riastrad uint64_t *const offset_table = malloc(n_offsets * sizeof(uint64_t));
111 1.1 riastrad
112 1.1 riastrad /* Read the offset table in. */
113 1.1 riastrad const ssize_t ot_read = read(cloop2_fd, offset_table,
114 1.1 riastrad (n_offsets * sizeof(uint64_t)));
115 1.1 riastrad if (ot_read == -1)
116 1.1 riastrad err(1, "read offset table");
117 1.1 riastrad assert(ot_read >= 0);
118 1.1 riastrad if ((size_t)ot_read != (n_offsets * sizeof(uint64_t)))
119 1.1 riastrad errx(1, "partial read of offset table: %zu", (size_t)ot_read);
120 1.1 riastrad
121 1.1 riastrad /* Allocate compression buffers. */
122 1.1 riastrad /* XXX compression ratio bound */
123 1.1 riastrad __CTASSERT(MAX_BLOCKSIZE <= (SIZE_MAX / 2));
124 1.1 riastrad void *const compbuf = malloc(2 * (size_t)blocksize);
125 1.1 riastrad if (compbuf == NULL)
126 1.1 riastrad err(1, "malloc compressed buffer");
127 1.1 riastrad
128 1.1 riastrad __CTASSERT(MAX_BLOCKSIZE <= SIZE_MAX);
129 1.1 riastrad void *const uncompbuf = malloc(blocksize);
130 1.1 riastrad if (uncompbuf == NULL)
131 1.1 riastrad err(1, "malloc uncompressed buffer");
132 1.1 riastrad
133 1.1 riastrad /*
134 1.1 riastrad * Uncompress the blocks.
135 1.1 riastrad */
136 1.1 riastrad __CTASSERT(MAX_N_OFFSETS <= (OFF_MAX / sizeof(uint64_t)));
137 1.1 riastrad __CTASSERT(sizeof(header) <=
138 1.1 riastrad (OFF_MAX - (MAX_N_OFFSETS * sizeof(uint64_t))));
139 1.1 riastrad __CTASSERT(OFF_MAX <= UINT64_MAX);
140 1.1 riastrad uint64_t offset = (sizeof(header) + (n_offsets * sizeof(uint64_t)));
141 1.1 riastrad uint32_t blkno;
142 1.1 riastrad for (blkno = 0; blkno < n_blocks; blkno++) {
143 1.1 riastrad const uint64_t start = be64toh(offset_table[blkno]);
144 1.1 riastrad const uint64_t end = be64toh(offset_table[blkno + 1]);
145 1.1 riastrad
146 1.1 riastrad /* Sanity-check the offsets. */
147 1.1 riastrad if (start != offset)
148 1.1 riastrad errx(1, "strange offset for block %"PRIu32": %"PRIu64,
149 1.1 riastrad blkno, start);
150 1.1 riastrad /* XXX compression ratio bound */
151 1.1 riastrad __CTASSERT(MAX_BLOCKSIZE <= (SIZE_MAX / 2));
152 1.1 riastrad if ((2 * (size_t)blocksize) <= (end - start))
153 1.1 riastrad errx(1, "block %"PRIu32" too large: %"PRIu64" bytes",
154 1.1 riastrad blkno, (end - start));
155 1.1 riastrad assert(offset <= MIN(OFF_MAX, UINT64_MAX));
156 1.1 riastrad if ((MIN(OFF_MAX, UINT64_MAX) - offset) < (end - start))
157 1.1 riastrad errx(1, "block %"PRIu32" overflows offset:"
158 1.1 riastrad " %"PRIu64" + %"PRIu64,
159 1.1 riastrad blkno, offset, (end - start));
160 1.1 riastrad
161 1.1 riastrad /* Read the compressed block. */
162 1.1 riastrad const ssize_t n_read = read(cloop2_fd, compbuf, (end - start));
163 1.1 riastrad if (n_read == -1)
164 1.1 riastrad err(1, "read block %"PRIu32, blkno);
165 1.1 riastrad assert(n_read >= 0);
166 1.1 riastrad if ((size_t)n_read != (end - start))
167 1.1 riastrad errx(1, "partial read of block %"PRIu32": %zu", blkno,
168 1.1 riastrad (size_t)n_read);
169 1.1 riastrad
170 1.1 riastrad /* Uncompress the block. */
171 1.1 riastrad const unsigned long complen = (end - start);
172 1.1 riastrad unsigned long uncomplen = blocksize;
173 1.1 riastrad const int zerror = uncompress(uncompbuf, &uncomplen, compbuf,
174 1.1 riastrad complen);
175 1.1 riastrad if (zerror != Z_OK)
176 1.1 riastrad errx(1, "block %"PRIu32" decompression failure (%d)"
177 1.1 riastrad ": %s", blkno, zerror, zError(zerror));
178 1.1 riastrad
179 1.1 riastrad /* Sanity-check the uncompressed length. */
180 1.1 riastrad assert(uncomplen <= blocksize);
181 1.1 riastrad if (((blkno + 1) < n_blocks) && (uncomplen != blocksize))
182 1.1 riastrad errx(1, "truncated non-final block %"PRIu32
183 1.1 riastrad ": %lu bytes", blkno, uncomplen);
184 1.1 riastrad
185 1.1 riastrad /* Write the uncompressed block. */
186 1.1 riastrad const ssize_t n_written = write(image_fd, uncompbuf,
187 1.1 riastrad uncomplen);
188 1.1 riastrad if (n_written == -1)
189 1.1 riastrad err(1, "write block %"PRIu32, blkno);
190 1.1 riastrad assert(n_written >= 0);
191 1.1 riastrad if ((size_t)n_written != uncomplen)
192 1.1 riastrad errx(1, "partial write of block %"PRIu32": %zu", blkno,
193 1.1 riastrad (size_t)n_written);
194 1.1 riastrad
195 1.1 riastrad /* Advance our position. */
196 1.1 riastrad assert((size_t)n_read <= (MIN(OFF_MAX, UINT64_MAX) - offset));
197 1.1 riastrad offset += (size_t)n_read;
198 1.1 riastrad }
199 1.1 riastrad
200 1.1 riastrad /* Free the offset table and compression buffers. */
201 1.1 riastrad free(offset_table);
202 1.1 riastrad free(uncompbuf);
203 1.1 riastrad free(compbuf);
204 1.1 riastrad
205 1.1 riastrad /* Close the files. */
206 1.1 riastrad if (close(image_fd) == -1)
207 1.1 riastrad warn("close(image fd)");
208 1.1 riastrad if (close(cloop2_fd) == -1)
209 1.1 riastrad warn("close(cloop2 fd)");
210 1.1 riastrad
211 1.1 riastrad return 0;
212 1.1 riastrad }
213