17f4caa8cSMaxim Sobolev /* 2*8f8cb840SMaxim Sobolev * Copyright (c) 2004-2016 Maxim Sobolev <sobomax@FreeBSD.org> 3*8f8cb840SMaxim Sobolev * All rights reserved. 47f4caa8cSMaxim Sobolev * 5*8f8cb840SMaxim Sobolev * Redistribution and use in source and binary forms, with or without 6*8f8cb840SMaxim Sobolev * modification, are permitted provided that the following conditions 7*8f8cb840SMaxim Sobolev * are met: 8*8f8cb840SMaxim Sobolev * 1. Redistributions of source code must retain the above copyright 9*8f8cb840SMaxim Sobolev * notice, this list of conditions and the following disclaimer. 10*8f8cb840SMaxim Sobolev * 2. Redistributions in binary form must reproduce the above copyright 11*8f8cb840SMaxim Sobolev * notice, this list of conditions and the following disclaimer in the 12*8f8cb840SMaxim Sobolev * documentation and/or other materials provided with the distribution. 13*8f8cb840SMaxim Sobolev * 14*8f8cb840SMaxim Sobolev * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15*8f8cb840SMaxim Sobolev * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16*8f8cb840SMaxim Sobolev * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17*8f8cb840SMaxim Sobolev * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18*8f8cb840SMaxim Sobolev * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19*8f8cb840SMaxim Sobolev * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20*8f8cb840SMaxim Sobolev * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21*8f8cb840SMaxim Sobolev * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22*8f8cb840SMaxim Sobolev * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23*8f8cb840SMaxim Sobolev * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24*8f8cb840SMaxim Sobolev * SUCH DAMAGE. 257f4caa8cSMaxim Sobolev * 267f4caa8cSMaxim Sobolev */ 277f4caa8cSMaxim Sobolev 28*8f8cb840SMaxim Sobolev #include <sys/cdefs.h> 29*8f8cb840SMaxim Sobolev __FBSDID("$FreeBSD$"); 30*8f8cb840SMaxim Sobolev 317f4caa8cSMaxim Sobolev #include <sys/types.h> 3227d0a1a4SMax Khon #include <sys/disk.h> 337f4caa8cSMaxim Sobolev #include <sys/endian.h> 347f4caa8cSMaxim Sobolev #include <sys/param.h> 357f4caa8cSMaxim Sobolev #include <sys/stat.h> 367f4caa8cSMaxim Sobolev #include <sys/uio.h> 377f4caa8cSMaxim Sobolev #include <netinet/in.h> 38*8f8cb840SMaxim Sobolev #include <ctype.h> 397f4caa8cSMaxim Sobolev #include <err.h> 407f4caa8cSMaxim Sobolev #include <fcntl.h> 417f4caa8cSMaxim Sobolev #include <signal.h> 427f4caa8cSMaxim Sobolev #include <stdio.h> 437f4caa8cSMaxim Sobolev #include <stdlib.h> 447f4caa8cSMaxim Sobolev #include <string.h> 457f4caa8cSMaxim Sobolev #include <unistd.h> 467f4caa8cSMaxim Sobolev 47*8f8cb840SMaxim Sobolev #include "mkuzip.h" 48*8f8cb840SMaxim Sobolev #include "mkuz_cloop.h" 49*8f8cb840SMaxim Sobolev #include "mkuz_blockcache.h" 50*8f8cb840SMaxim Sobolev #include "mkuz_zlib.h" 51*8f8cb840SMaxim Sobolev #include "mkuz_lzma.h" 527f4caa8cSMaxim Sobolev 53*8f8cb840SMaxim Sobolev #define DEFINE_RAW_METHOD(func, rval, args...) typedef rval (*func##_t)(args) 54*8f8cb840SMaxim Sobolev 55*8f8cb840SMaxim Sobolev #define DEFAULT_CLSTSIZE 16384 56*8f8cb840SMaxim Sobolev 57*8f8cb840SMaxim Sobolev DEFINE_RAW_METHOD(f_init, void *, uint32_t); 58*8f8cb840SMaxim Sobolev DEFINE_RAW_METHOD(f_compress, void, const char *, uint32_t *); 59*8f8cb840SMaxim Sobolev 60*8f8cb840SMaxim Sobolev struct mkuz_format { 61*8f8cb840SMaxim Sobolev const char *magic; 62*8f8cb840SMaxim Sobolev const char *default_sufx; 63*8f8cb840SMaxim Sobolev f_init_t f_init; 64*8f8cb840SMaxim Sobolev f_compress_t f_compress; 65*8f8cb840SMaxim Sobolev }; 66*8f8cb840SMaxim Sobolev 67*8f8cb840SMaxim Sobolev static struct mkuz_format uzip_fmt = { 68*8f8cb840SMaxim Sobolev .magic = CLOOP_MAGIC_ZLIB, 69*8f8cb840SMaxim Sobolev .default_sufx = DEFAULT_SUFX_ZLIB, 70*8f8cb840SMaxim Sobolev .f_init = &mkuz_zlib_init, 71*8f8cb840SMaxim Sobolev .f_compress = &mkuz_zlib_compress 72*8f8cb840SMaxim Sobolev }; 73*8f8cb840SMaxim Sobolev 74*8f8cb840SMaxim Sobolev static struct mkuz_format ulzma_fmt = { 75*8f8cb840SMaxim Sobolev .magic = CLOOP_MAGIC_LZMA, 76*8f8cb840SMaxim Sobolev .default_sufx = DEFAULT_SUFX_LZMA, 77*8f8cb840SMaxim Sobolev .f_init = &mkuz_lzma_init, 78*8f8cb840SMaxim Sobolev .f_compress = &mkuz_lzma_compress 79*8f8cb840SMaxim Sobolev }; 807f4caa8cSMaxim Sobolev 817f4caa8cSMaxim Sobolev static char *readblock(int, char *, u_int32_t); 827f4caa8cSMaxim Sobolev static void usage(void); 837f4caa8cSMaxim Sobolev static void cleanup(void); 84*8f8cb840SMaxim Sobolev static int memvcmp(const void *, unsigned char, size_t); 857f4caa8cSMaxim Sobolev 867f4caa8cSMaxim Sobolev static char *cleanfile = NULL; 877f4caa8cSMaxim Sobolev 887f4caa8cSMaxim Sobolev int main(int argc, char **argv) 897f4caa8cSMaxim Sobolev { 907f4caa8cSMaxim Sobolev char *iname, *oname, *obuf, *ibuf; 917f4caa8cSMaxim Sobolev uint64_t *toc; 92*8f8cb840SMaxim Sobolev int fdr, fdw, i, opt, verbose, no_zcomp, tmp, en_dedup; 937f4caa8cSMaxim Sobolev struct iovec iov[2]; 947f4caa8cSMaxim Sobolev struct stat sb; 95*8f8cb840SMaxim Sobolev uint32_t destlen; 96*8f8cb840SMaxim Sobolev uint64_t offset, last_offset; 97*8f8cb840SMaxim Sobolev struct cloop_header hdr; 98*8f8cb840SMaxim Sobolev struct mkuz_blkcache_hit *chit; 99*8f8cb840SMaxim Sobolev const struct mkuz_format *handler; 1007f4caa8cSMaxim Sobolev 1017f4caa8cSMaxim Sobolev memset(&hdr, 0, sizeof(hdr)); 102*8f8cb840SMaxim Sobolev hdr.blksz = DEFAULT_CLSTSIZE; 1037f4caa8cSMaxim Sobolev oname = NULL; 1047f4caa8cSMaxim Sobolev verbose = 0; 105*8f8cb840SMaxim Sobolev no_zcomp = 0; 106*8f8cb840SMaxim Sobolev en_dedup = 0; 107*8f8cb840SMaxim Sobolev handler = &uzip_fmt; 1087f4caa8cSMaxim Sobolev 109*8f8cb840SMaxim Sobolev while((opt = getopt(argc, argv, "o:s:vZdL")) != -1) { 1107f4caa8cSMaxim Sobolev switch(opt) { 1117f4caa8cSMaxim Sobolev case 'o': 1127f4caa8cSMaxim Sobolev oname = optarg; 1137f4caa8cSMaxim Sobolev break; 1147f4caa8cSMaxim Sobolev 1157f4caa8cSMaxim Sobolev case 's': 1167f4caa8cSMaxim Sobolev tmp = atoi(optarg); 1177f4caa8cSMaxim Sobolev if (tmp <= 0) { 1187f4caa8cSMaxim Sobolev errx(1, "invalid cluster size specified: %s", 1197f4caa8cSMaxim Sobolev optarg); 1207f4caa8cSMaxim Sobolev /* Not reached */ 1217f4caa8cSMaxim Sobolev } 1227f4caa8cSMaxim Sobolev hdr.blksz = tmp; 1237f4caa8cSMaxim Sobolev break; 1247f4caa8cSMaxim Sobolev 1257f4caa8cSMaxim Sobolev case 'v': 1267f4caa8cSMaxim Sobolev verbose = 1; 1277f4caa8cSMaxim Sobolev break; 1287f4caa8cSMaxim Sobolev 129*8f8cb840SMaxim Sobolev case 'Z': 130*8f8cb840SMaxim Sobolev no_zcomp = 1; 131*8f8cb840SMaxim Sobolev break; 132*8f8cb840SMaxim Sobolev 133*8f8cb840SMaxim Sobolev case 'd': 134*8f8cb840SMaxim Sobolev en_dedup = 1; 135*8f8cb840SMaxim Sobolev break; 136*8f8cb840SMaxim Sobolev 137*8f8cb840SMaxim Sobolev case 'L': 138*8f8cb840SMaxim Sobolev handler = &ulzma_fmt; 139*8f8cb840SMaxim Sobolev break; 140*8f8cb840SMaxim Sobolev 1417f4caa8cSMaxim Sobolev default: 1427f4caa8cSMaxim Sobolev usage(); 1437f4caa8cSMaxim Sobolev /* Not reached */ 1447f4caa8cSMaxim Sobolev } 1457f4caa8cSMaxim Sobolev } 1467f4caa8cSMaxim Sobolev argc -= optind; 1477f4caa8cSMaxim Sobolev argv += optind; 1487f4caa8cSMaxim Sobolev 1497f4caa8cSMaxim Sobolev if (argc != 1) { 1507f4caa8cSMaxim Sobolev usage(); 1517f4caa8cSMaxim Sobolev /* Not reached */ 1527f4caa8cSMaxim Sobolev } 1537f4caa8cSMaxim Sobolev 154*8f8cb840SMaxim Sobolev strcpy(hdr.magic, handler->magic); 155*8f8cb840SMaxim Sobolev 156*8f8cb840SMaxim Sobolev if (en_dedup != 0) { 157*8f8cb840SMaxim Sobolev hdr.magic[CLOOP_OFS_VERSN] = CLOOP_MAJVER_3; 158*8f8cb840SMaxim Sobolev hdr.magic[CLOOP_OFS_COMPR] = 159*8f8cb840SMaxim Sobolev tolower(hdr.magic[CLOOP_OFS_COMPR]); 160*8f8cb840SMaxim Sobolev } 161*8f8cb840SMaxim Sobolev 162*8f8cb840SMaxim Sobolev obuf = handler->f_init(hdr.blksz); 163*8f8cb840SMaxim Sobolev 1647f4caa8cSMaxim Sobolev iname = argv[0]; 1657f4caa8cSMaxim Sobolev if (oname == NULL) { 166*8f8cb840SMaxim Sobolev asprintf(&oname, "%s%s", iname, handler->default_sufx); 1677f4caa8cSMaxim Sobolev if (oname == NULL) { 1687f4caa8cSMaxim Sobolev err(1, "can't allocate memory"); 1697f4caa8cSMaxim Sobolev /* Not reached */ 1707f4caa8cSMaxim Sobolev } 1717f4caa8cSMaxim Sobolev } 1727f4caa8cSMaxim Sobolev 173*8f8cb840SMaxim Sobolev ibuf = mkuz_safe_malloc(hdr.blksz); 1747f4caa8cSMaxim Sobolev 1757f4caa8cSMaxim Sobolev signal(SIGHUP, exit); 1767f4caa8cSMaxim Sobolev signal(SIGINT, exit); 1777f4caa8cSMaxim Sobolev signal(SIGTERM, exit); 1787f4caa8cSMaxim Sobolev signal(SIGXCPU, exit); 1797f4caa8cSMaxim Sobolev signal(SIGXFSZ, exit); 1807f4caa8cSMaxim Sobolev atexit(cleanup); 1817f4caa8cSMaxim Sobolev 18227d0a1a4SMax Khon fdr = open(iname, O_RDONLY); 18327d0a1a4SMax Khon if (fdr < 0) { 18427d0a1a4SMax Khon err(1, "open(%s)", iname); 1857f4caa8cSMaxim Sobolev /* Not reached */ 1867f4caa8cSMaxim Sobolev } 18727d0a1a4SMax Khon if (fstat(fdr, &sb) != 0) { 18827d0a1a4SMax Khon err(1, "fstat(%s)", iname); 18927d0a1a4SMax Khon /* Not reached */ 19027d0a1a4SMax Khon } 19127d0a1a4SMax Khon if (S_ISCHR(sb.st_mode)) { 19227d0a1a4SMax Khon off_t ms; 19327d0a1a4SMax Khon 19427d0a1a4SMax Khon if (ioctl(fdr, DIOCGMEDIASIZE, &ms) < 0) { 19527d0a1a4SMax Khon err(1, "ioctl(DIOCGMEDIASIZE)"); 19627d0a1a4SMax Khon /* Not reached */ 19727d0a1a4SMax Khon } 19827d0a1a4SMax Khon sb.st_size = ms; 19927d0a1a4SMax Khon } else if (!S_ISREG(sb.st_mode)) { 20027d0a1a4SMax Khon fprintf(stderr, "%s: not a character device or regular file\n", 20127d0a1a4SMax Khon iname); 20227d0a1a4SMax Khon exit(1); 20327d0a1a4SMax Khon } 2047f4caa8cSMaxim Sobolev hdr.nblocks = sb.st_size / hdr.blksz; 2050b99ac63SMaxim Sobolev if ((sb.st_size % hdr.blksz) != 0) { 2060b99ac63SMaxim Sobolev if (verbose != 0) 2070b99ac63SMaxim Sobolev fprintf(stderr, "file size is not multiple " 2080b99ac63SMaxim Sobolev "of %d, padding data\n", hdr.blksz); 2090b99ac63SMaxim Sobolev hdr.nblocks++; 2100b99ac63SMaxim Sobolev } 211*8f8cb840SMaxim Sobolev toc = mkuz_safe_malloc((hdr.nblocks + 1) * sizeof(*toc)); 2127f4caa8cSMaxim Sobolev 2137f4caa8cSMaxim Sobolev fdw = open(oname, O_WRONLY | O_TRUNC | O_CREAT, 2145cf3bf70SMax Khon S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); 2157f4caa8cSMaxim Sobolev if (fdw < 0) { 216d72d8f53SPawel Jakub Dawidek err(1, "open(%s)", oname); 2177f4caa8cSMaxim Sobolev /* Not reached */ 2187f4caa8cSMaxim Sobolev } 2197f4caa8cSMaxim Sobolev cleanfile = oname; 2207f4caa8cSMaxim Sobolev 2217f4caa8cSMaxim Sobolev /* Prepare header that we will write later when we have index ready. */ 2227f4caa8cSMaxim Sobolev iov[0].iov_base = (char *)&hdr; 2237f4caa8cSMaxim Sobolev iov[0].iov_len = sizeof(hdr); 2247f4caa8cSMaxim Sobolev iov[1].iov_base = (char *)toc; 2257f4caa8cSMaxim Sobolev iov[1].iov_len = (hdr.nblocks + 1) * sizeof(*toc); 2267f4caa8cSMaxim Sobolev offset = iov[0].iov_len + iov[1].iov_len; 2277f4caa8cSMaxim Sobolev 2287f4caa8cSMaxim Sobolev /* Reserve space for header */ 2297f4caa8cSMaxim Sobolev lseek(fdw, offset, SEEK_SET); 2307f4caa8cSMaxim Sobolev 2317f4caa8cSMaxim Sobolev if (verbose != 0) 232ed9302fdSMaxim Sobolev fprintf(stderr, "data size %ju bytes, number of clusters " 2335cf3bf70SMax Khon "%u, index length %zu bytes\n", sb.st_size, 2340b99ac63SMaxim Sobolev hdr.nblocks, iov[1].iov_len); 2357f4caa8cSMaxim Sobolev 236*8f8cb840SMaxim Sobolev last_offset = 0; 2377f4caa8cSMaxim Sobolev for(i = 0; i == 0 || ibuf != NULL; i++) { 2387f4caa8cSMaxim Sobolev ibuf = readblock(fdr, ibuf, hdr.blksz); 2397f4caa8cSMaxim Sobolev if (ibuf != NULL) { 240*8f8cb840SMaxim Sobolev if (no_zcomp == 0 && \ 241*8f8cb840SMaxim Sobolev memvcmp(ibuf, '\0', hdr.blksz) != 0) { 242*8f8cb840SMaxim Sobolev /* All zeroes block */ 243*8f8cb840SMaxim Sobolev destlen = 0; 244*8f8cb840SMaxim Sobolev } else { 245*8f8cb840SMaxim Sobolev handler->f_compress(ibuf, &destlen); 2467f4caa8cSMaxim Sobolev } 2477f4caa8cSMaxim Sobolev } else { 2487f4caa8cSMaxim Sobolev destlen = DEV_BSIZE - (offset % DEV_BSIZE); 2497f4caa8cSMaxim Sobolev memset(obuf, 0, destlen); 2500b99ac63SMaxim Sobolev if (verbose != 0) 251*8f8cb840SMaxim Sobolev fprintf(stderr, "padding data with %lu bytes " 252*8f8cb840SMaxim Sobolev "so that file size is multiple of %d\n", 253*8f8cb840SMaxim Sobolev (u_long)destlen, DEV_BSIZE); 2547f4caa8cSMaxim Sobolev } 255*8f8cb840SMaxim Sobolev if (destlen > 0 && en_dedup != 0) { 256*8f8cb840SMaxim Sobolev chit = mkuz_blkcache_regblock(fdw, i, offset, destlen, 257*8f8cb840SMaxim Sobolev obuf); 258*8f8cb840SMaxim Sobolev /* 259*8f8cb840SMaxim Sobolev * There should be at least one non-empty block 260*8f8cb840SMaxim Sobolev * between us and the backref'ed offset, otherwise 261*8f8cb840SMaxim Sobolev * we won't be able to parse that sequence correctly 262*8f8cb840SMaxim Sobolev * as it would be indistinguishible from another 263*8f8cb840SMaxim Sobolev * empty block. 264*8f8cb840SMaxim Sobolev */ 265*8f8cb840SMaxim Sobolev if (chit != NULL && chit->offset == last_offset) { 266*8f8cb840SMaxim Sobolev chit = NULL; 267*8f8cb840SMaxim Sobolev } 268*8f8cb840SMaxim Sobolev } else { 269*8f8cb840SMaxim Sobolev chit = NULL; 270*8f8cb840SMaxim Sobolev } 271*8f8cb840SMaxim Sobolev if (chit != NULL) { 272*8f8cb840SMaxim Sobolev toc[i] = htobe64(chit->offset); 273*8f8cb840SMaxim Sobolev } else { 274*8f8cb840SMaxim Sobolev if (destlen > 0 && write(fdw, obuf, destlen) < 0) { 275d72d8f53SPawel Jakub Dawidek err(1, "write(%s)", oname); 2767f4caa8cSMaxim Sobolev /* Not reached */ 2777f4caa8cSMaxim Sobolev } 2787f4caa8cSMaxim Sobolev toc[i] = htobe64(offset); 279*8f8cb840SMaxim Sobolev last_offset = offset; 2807f4caa8cSMaxim Sobolev offset += destlen; 2817f4caa8cSMaxim Sobolev } 282*8f8cb840SMaxim Sobolev if (ibuf != NULL && verbose != 0) { 283*8f8cb840SMaxim Sobolev fprintf(stderr, "cluster #%d, in %u bytes, " 284*8f8cb840SMaxim Sobolev "out len=%lu offset=%lu", i, hdr.blksz, 285*8f8cb840SMaxim Sobolev chit == NULL ? (u_long)destlen : 0, 286*8f8cb840SMaxim Sobolev (u_long)be64toh(toc[i])); 287*8f8cb840SMaxim Sobolev if (chit != NULL) { 288*8f8cb840SMaxim Sobolev fprintf(stderr, " (backref'ed to #%d)", 289*8f8cb840SMaxim Sobolev chit->blkno); 290*8f8cb840SMaxim Sobolev } 291*8f8cb840SMaxim Sobolev fprintf(stderr, "\n"); 292*8f8cb840SMaxim Sobolev 293*8f8cb840SMaxim Sobolev } 294*8f8cb840SMaxim Sobolev } 2957f4caa8cSMaxim Sobolev close(fdr); 2967f4caa8cSMaxim Sobolev 2977f4caa8cSMaxim Sobolev if (verbose != 0) 298ed9302fdSMaxim Sobolev fprintf(stderr, "compressed data to %ju bytes, saved %lld " 299*8f8cb840SMaxim Sobolev "bytes, %.2f%% decrease.\n", offset, 300*8f8cb840SMaxim Sobolev (long long)(sb.st_size - offset), 301*8f8cb840SMaxim Sobolev 100.0 * (long long)(sb.st_size - offset) / 302*8f8cb840SMaxim Sobolev (float)sb.st_size); 3037f4caa8cSMaxim Sobolev 3047f4caa8cSMaxim Sobolev /* Convert to big endian */ 3057f4caa8cSMaxim Sobolev hdr.blksz = htonl(hdr.blksz); 3067f4caa8cSMaxim Sobolev hdr.nblocks = htonl(hdr.nblocks); 3077f4caa8cSMaxim Sobolev /* Write headers into pre-allocated space */ 3087f4caa8cSMaxim Sobolev lseek(fdw, 0, SEEK_SET); 3097f4caa8cSMaxim Sobolev if (writev(fdw, iov, 2) < 0) { 310d72d8f53SPawel Jakub Dawidek err(1, "writev(%s)", oname); 3117f4caa8cSMaxim Sobolev /* Not reached */ 3127f4caa8cSMaxim Sobolev } 3137f4caa8cSMaxim Sobolev cleanfile = NULL; 3147f4caa8cSMaxim Sobolev close(fdw); 3157f4caa8cSMaxim Sobolev 3167f4caa8cSMaxim Sobolev exit(0); 3177f4caa8cSMaxim Sobolev } 3187f4caa8cSMaxim Sobolev 3197f4caa8cSMaxim Sobolev static char * 3200b99ac63SMaxim Sobolev readblock(int fd, char *ibuf, u_int32_t clstsize) 3210b99ac63SMaxim Sobolev { 3227f4caa8cSMaxim Sobolev int numread; 3237f4caa8cSMaxim Sobolev 3247f4caa8cSMaxim Sobolev bzero(ibuf, clstsize); 3257f4caa8cSMaxim Sobolev numread = read(fd, ibuf, clstsize); 3267f4caa8cSMaxim Sobolev if (numread < 0) { 3277f4caa8cSMaxim Sobolev err(1, "read() failed"); 3287f4caa8cSMaxim Sobolev /* Not reached */ 3297f4caa8cSMaxim Sobolev } 3307f4caa8cSMaxim Sobolev if (numread == 0) { 3317f4caa8cSMaxim Sobolev return NULL; 3327f4caa8cSMaxim Sobolev } 3337f4caa8cSMaxim Sobolev return ibuf; 3347f4caa8cSMaxim Sobolev } 3357f4caa8cSMaxim Sobolev 3367f4caa8cSMaxim Sobolev static void 3370b99ac63SMaxim Sobolev usage(void) 3380b99ac63SMaxim Sobolev { 3397f4caa8cSMaxim Sobolev 340*8f8cb840SMaxim Sobolev fprintf(stderr, "usage: mkuzip [-vZdL] [-o outfile] [-s cluster_size] " 341*8f8cb840SMaxim Sobolev "infile\n"); 3427f4caa8cSMaxim Sobolev exit(1); 3437f4caa8cSMaxim Sobolev } 3447f4caa8cSMaxim Sobolev 345*8f8cb840SMaxim Sobolev void * 346*8f8cb840SMaxim Sobolev mkuz_safe_malloc(size_t size) 3470b99ac63SMaxim Sobolev { 3487f4caa8cSMaxim Sobolev void *retval; 3497f4caa8cSMaxim Sobolev 3507f4caa8cSMaxim Sobolev retval = malloc(size); 3517f4caa8cSMaxim Sobolev if (retval == NULL) { 3527f4caa8cSMaxim Sobolev err(1, "can't allocate memory"); 3537f4caa8cSMaxim Sobolev /* Not reached */ 3547f4caa8cSMaxim Sobolev } 3557f4caa8cSMaxim Sobolev return retval; 3567f4caa8cSMaxim Sobolev } 3577f4caa8cSMaxim Sobolev 3587f4caa8cSMaxim Sobolev static void 3590b99ac63SMaxim Sobolev cleanup(void) 3600b99ac63SMaxim Sobolev { 3617f4caa8cSMaxim Sobolev 3627f4caa8cSMaxim Sobolev if (cleanfile != NULL) 3637f4caa8cSMaxim Sobolev unlink(cleanfile); 3647f4caa8cSMaxim Sobolev } 365*8f8cb840SMaxim Sobolev 366*8f8cb840SMaxim Sobolev static int 367*8f8cb840SMaxim Sobolev memvcmp(const void *memory, unsigned char val, size_t size) 368*8f8cb840SMaxim Sobolev { 369*8f8cb840SMaxim Sobolev const u_char *mm; 370*8f8cb840SMaxim Sobolev 371*8f8cb840SMaxim Sobolev mm = (const u_char *)memory; 372*8f8cb840SMaxim Sobolev return (*mm == val) && memcmp(mm, mm + 1, size - 1) == 0; 373*8f8cb840SMaxim Sobolev } 374