2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <sobomax@FreeBSD.ORG> wrote this file. As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return. Maxim Sobolev
7 * ----------------------------------------------------------------------------
13 #include <sys/types.h>
14 #include <sys/endian.h>
15 #include <sys/param.h>
18 #include <netinet/in.h>
28 #define CLSTSIZE 16384
29 #define DEFAULT_SUFX ".uzip"
31 #define CLOOP_MAGIC_LEN 128
32 static char CLOOP_MAGIC_START[] = "#!/bin/sh\n#V2.0 Format\n"
33 "m=geom_uzip\n(kldstat -m $m 2>&-||kldload $m)>&-&&"
34 "mount_cd9660 /dev/`mdconfig -af $0`.uzip $1\nexit $?\n";
36 static char *readblock(int, char *, u_int32_t);
37 static void usage(void);
38 static void *safe_malloc(size_t);
39 static void cleanup(void);
41 static char *cleanfile = NULL;
43 int main(int argc, char **argv)
45 char *iname, *oname, *obuf, *ibuf;
47 int fdr, fdw, i, opt, verbose, tmp;
53 char magic[CLOOP_MAGIC_LEN]; /* cloop magic */
54 uint32_t blksz; /* block size */
55 uint32_t nblocks; /* number of blocks */
58 memset(&hdr, 0, sizeof(hdr));
60 strcpy(hdr.magic, CLOOP_MAGIC_START);
64 while((opt = getopt(argc, argv, "o:s:v")) != -1) {
73 errx(1, "invalid cluster size specified: %s",
77 if (tmp % DEV_BSIZE != 0) {
78 errx(1, "cluster size should be multiple of %d",
82 if (compressBound(tmp) > MAXPHYS) {
83 errx(1, "cluster size is too large");
108 asprintf(&oname, "%s%s", iname, DEFAULT_SUFX);
110 err(1, "can't allocate memory");
115 obuf = safe_malloc(compressBound(hdr.blksz));
116 ibuf = safe_malloc(hdr.blksz);
118 signal(SIGHUP, exit);
119 signal(SIGINT, exit);
120 signal(SIGTERM, exit);
121 signal(SIGXCPU, exit);
122 signal(SIGXFSZ, exit);
125 if (stat(iname, &sb) != 0) {
126 err(1, "stat(%s)", iname);
129 hdr.nblocks = sb.st_size / hdr.blksz;
130 if ((sb.st_size % hdr.blksz) != 0) {
132 fprintf(stderr, "file size is not multiple "
133 "of %d, padding data\n", hdr.blksz);
136 toc = safe_malloc((hdr.nblocks + 1) * sizeof(*toc));
138 fdr = open(iname, O_RDONLY);
140 err(1, "open(%s)", iname);
143 fdw = open(oname, O_WRONLY | O_TRUNC | O_CREAT,
144 S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
146 err(1, "open(%s)", oname);
151 /* Prepare header that we will write later when we have index ready. */
152 iov[0].iov_base = (char *)&hdr;
153 iov[0].iov_len = sizeof(hdr);
154 iov[1].iov_base = (char *)toc;
155 iov[1].iov_len = (hdr.nblocks + 1) * sizeof(*toc);
156 offset = iov[0].iov_len + iov[1].iov_len;
158 /* Reserve space for header */
159 lseek(fdw, offset, SEEK_SET);
162 fprintf(stderr, "data size %ju bytes, number of clusters "
163 "%u, index length %zu bytes\n", sb.st_size,
164 hdr.nblocks, iov[1].iov_len);
166 for(i = 0; i == 0 || ibuf != NULL; i++) {
167 ibuf = readblock(fdr, ibuf, hdr.blksz);
169 destlen = compressBound(hdr.blksz);
170 if (compress2(obuf, &destlen, ibuf, hdr.blksz,
171 Z_BEST_COMPRESSION) != Z_OK) {
172 errx(1, "can't compress data: compress2() "
177 fprintf(stderr, "cluster #%d, in %u bytes, "
178 "out %lu bytes\n", i, hdr.blksz, destlen);
180 destlen = DEV_BSIZE - (offset % DEV_BSIZE);
181 memset(obuf, 0, destlen);
183 fprintf(stderr, "padding data with %lu bytes so "
184 "that file size is multiple of %d\n", destlen,
187 if (write(fdw, obuf, destlen) < 0) {
188 err(1, "write(%s)", oname);
191 toc[i] = htobe64(offset);
197 fprintf(stderr, "compressed data to %ju bytes, saved %lld "
198 "bytes, %.2f%% decrease.\n", offset, (long long)(sb.st_size - offset),
199 100.0 * (long long)(sb.st_size - offset) / (float)sb.st_size);
201 /* Convert to big endian */
202 hdr.blksz = htonl(hdr.blksz);
203 hdr.nblocks = htonl(hdr.nblocks);
204 /* Write headers into pre-allocated space */
205 lseek(fdw, 0, SEEK_SET);
206 if (writev(fdw, iov, 2) < 0) {
207 err(1, "writev(%s)", oname);
217 readblock(int fd, char *ibuf, u_int32_t clstsize)
221 bzero(ibuf, clstsize);
222 numread = read(fd, ibuf, clstsize);
224 err(1, "read() failed");
237 fprintf(stderr, "usage: mkuzip [-v] [-o outfile] [-s cluster_size] infile\n");
242 safe_malloc(size_t size)
246 retval = malloc(size);
247 if (retval == NULL) {
248 err(1, "can't allocate memory");
258 if (cleanfile != NULL)