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>
15 #include <sys/endian.h>
16 #include <sys/param.h>
19 #include <netinet/in.h>
29 #define CLSTSIZE 16384
30 #define DEFAULT_SUFX ".uzip"
32 #define CLOOP_MAGIC_LEN 128
33 static char CLOOP_MAGIC_START[] = "#!/bin/sh\n#V2.0 Format\n"
34 "m=geom_uzip\n(kldstat -m $m 2>&-||kldload $m)>&-&&"
35 "mount_cd9660 /dev/`mdconfig -af $0`.uzip $1\nexit $?\n";
37 static char *readblock(int, char *, u_int32_t);
38 static void usage(void);
39 static void *safe_malloc(size_t);
40 static void cleanup(void);
42 static char *cleanfile = NULL;
44 int main(int argc, char **argv)
46 char *iname, *oname, *obuf, *ibuf;
48 int fdr, fdw, i, opt, verbose, tmp;
54 char magic[CLOOP_MAGIC_LEN]; /* cloop magic */
55 uint32_t blksz; /* block size */
56 uint32_t nblocks; /* number of blocks */
59 memset(&hdr, 0, sizeof(hdr));
61 strcpy(hdr.magic, CLOOP_MAGIC_START);
65 while((opt = getopt(argc, argv, "o:s:v")) != -1) {
74 errx(1, "invalid cluster size specified: %s",
78 if (tmp % DEV_BSIZE != 0) {
79 errx(1, "cluster size should be multiple of %d",
83 if (compressBound(tmp) > MAXPHYS) {
84 errx(1, "cluster size is too large");
109 asprintf(&oname, "%s%s", iname, DEFAULT_SUFX);
111 err(1, "can't allocate memory");
116 obuf = safe_malloc(compressBound(hdr.blksz));
117 ibuf = safe_malloc(hdr.blksz);
119 signal(SIGHUP, exit);
120 signal(SIGINT, exit);
121 signal(SIGTERM, exit);
122 signal(SIGXCPU, exit);
123 signal(SIGXFSZ, exit);
126 fdr = open(iname, O_RDONLY);
128 err(1, "open(%s)", iname);
131 if (fstat(fdr, &sb) != 0) {
132 err(1, "fstat(%s)", iname);
135 if (S_ISCHR(sb.st_mode)) {
138 if (ioctl(fdr, DIOCGMEDIASIZE, &ms) < 0) {
139 err(1, "ioctl(DIOCGMEDIASIZE)");
143 } else if (!S_ISREG(sb.st_mode)) {
144 fprintf(stderr, "%s: not a character device or regular file\n",
148 hdr.nblocks = sb.st_size / hdr.blksz;
149 if ((sb.st_size % hdr.blksz) != 0) {
151 fprintf(stderr, "file size is not multiple "
152 "of %d, padding data\n", hdr.blksz);
155 toc = safe_malloc((hdr.nblocks + 1) * sizeof(*toc));
157 fdw = open(oname, O_WRONLY | O_TRUNC | O_CREAT,
158 S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
160 err(1, "open(%s)", oname);
165 /* Prepare header that we will write later when we have index ready. */
166 iov[0].iov_base = (char *)&hdr;
167 iov[0].iov_len = sizeof(hdr);
168 iov[1].iov_base = (char *)toc;
169 iov[1].iov_len = (hdr.nblocks + 1) * sizeof(*toc);
170 offset = iov[0].iov_len + iov[1].iov_len;
172 /* Reserve space for header */
173 lseek(fdw, offset, SEEK_SET);
176 fprintf(stderr, "data size %ju bytes, number of clusters "
177 "%u, index length %zu bytes\n", sb.st_size,
178 hdr.nblocks, iov[1].iov_len);
180 for(i = 0; i == 0 || ibuf != NULL; i++) {
181 ibuf = readblock(fdr, ibuf, hdr.blksz);
183 destlen = compressBound(hdr.blksz);
184 if (compress2(obuf, &destlen, ibuf, hdr.blksz,
185 Z_BEST_COMPRESSION) != Z_OK) {
186 errx(1, "can't compress data: compress2() "
191 fprintf(stderr, "cluster #%d, in %u bytes, "
192 "out %lu bytes\n", i, hdr.blksz, destlen);
194 destlen = DEV_BSIZE - (offset % DEV_BSIZE);
195 memset(obuf, 0, destlen);
197 fprintf(stderr, "padding data with %lu bytes so "
198 "that file size is multiple of %d\n", destlen,
201 if (write(fdw, obuf, destlen) < 0) {
202 err(1, "write(%s)", oname);
205 toc[i] = htobe64(offset);
211 fprintf(stderr, "compressed data to %ju bytes, saved %lld "
212 "bytes, %.2f%% decrease.\n", offset, (long long)(sb.st_size - offset),
213 100.0 * (long long)(sb.st_size - offset) / (float)sb.st_size);
215 /* Convert to big endian */
216 hdr.blksz = htonl(hdr.blksz);
217 hdr.nblocks = htonl(hdr.nblocks);
218 /* Write headers into pre-allocated space */
219 lseek(fdw, 0, SEEK_SET);
220 if (writev(fdw, iov, 2) < 0) {
221 err(1, "writev(%s)", oname);
231 readblock(int fd, char *ibuf, u_int32_t clstsize)
235 bzero(ibuf, clstsize);
236 numread = read(fd, ibuf, clstsize);
238 err(1, "read() failed");
251 fprintf(stderr, "usage: mkuzip [-v] [-o outfile] [-s cluster_size] infile\n");
256 safe_malloc(size_t size)
260 retval = malloc(size);
261 if (retval == NULL) {
262 err(1, "can't allocate memory");
272 if (cleanfile != NULL)