2 * ----------------------------------------------------------------------------
3 * Derived from mkuzip.c by Aleksandr Rybalko <ray@ddteam.net>
4 * ----------------------------------------------------------------------------
5 * "THE BEER-WARE LICENSE" (Revision 42):
6 * <sobomax@FreeBSD.ORG> wrote this file. As long as you retain this notice you
7 * can do whatever you want with this stuff. If we meet some day, and you think
8 * this stuff is worth it, you can buy me a beer in return. Maxim Sobolev
9 * ----------------------------------------------------------------------------
16 #include <sys/endian.h>
17 #include <sys/param.h>
20 #include <netinet/in.h>
30 #define CLSTSIZE 16384
31 #define DEFAULT_SUFX ".ulzma"
33 #define USED_BLOCKSIZE DEV_BSIZE
35 #define CLOOP_MAGIC_LEN 128
36 /* Format L3.0, since we move to XZ API */
37 static char CLOOP_MAGIC_START[] =
42 "(kldstat -m $m 2>&-||kldload $m)>&-&&"
43 "mount_cd9660 /dev/`mdconfig -af $0`.$n $1\n"
46 static char *readblock(int, char *, u_int32_t);
47 static void usage(void);
48 static void *safe_malloc(size_t);
49 static void cleanup(void);
51 static char *cleanfile = NULL;
53 int main(int argc, char **argv)
55 char *iname, *oname, *obuf, *ibuf;
56 int fdr, fdw, i, opt, verbose, tmp;
62 lzma_filter filters[2];
63 lzma_options_lzma opt_lzma;
65 lzma_stream strm = LZMA_STREAM_INIT;
67 char magic[CLOOP_MAGIC_LEN]; /* cloop magic */
68 uint32_t blksz; /* block size */
69 uint32_t nblocks; /* number of blocks */
72 memset(&hdr, 0, sizeof(hdr));
74 strcpy(hdr.magic, CLOOP_MAGIC_START);
78 while((opt = getopt(argc, argv, "o:s:v")) != -1) {
88 "invalid cluster size specified: %s",
92 if (tmp % USED_BLOCKSIZE != 0) {
94 "cluster size should be multiple of %d",
99 errx(1, "cluster size is too large");
124 asprintf(&oname, "%s%s", iname, DEFAULT_SUFX);
126 err(1, "can't allocate memory");
131 obuf = safe_malloc(hdr.blksz*2);
132 ibuf = safe_malloc(hdr.blksz);
134 signal(SIGHUP, exit);
135 signal(SIGINT, exit);
136 signal(SIGTERM, exit);
137 signal(SIGXCPU, exit);
138 signal(SIGXFSZ, exit);
141 fdr = open(iname, O_RDONLY);
143 err(1, "open(%s)", iname);
146 if (fstat(fdr, &sb) != 0) {
147 err(1, "fstat(%s)", iname);
150 if (S_ISCHR(sb.st_mode)) {
153 if (ioctl(fdr, DIOCGMEDIASIZE, &ms) < 0) {
154 err(1, "ioctl(DIOCGMEDIASIZE)");
158 } else if (!S_ISREG(sb.st_mode)) {
160 "%s: not a character device or regular file\n",
164 hdr.nblocks = sb.st_size / hdr.blksz;
165 if ((sb.st_size % hdr.blksz) != 0) {
167 fprintf(stderr, "file size is not multiple "
168 "of %d, padding data\n", hdr.blksz);
171 toc = safe_malloc((hdr.nblocks + 1) * sizeof(*toc));
173 fdw = open(oname, O_WRONLY | O_TRUNC | O_CREAT,
174 S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
176 err(1, "open(%s)", oname);
182 * Prepare header that we will write later when we have index ready.
184 iov[0].iov_base = (char *)&hdr;
185 iov[0].iov_len = sizeof(hdr);
186 iov[1].iov_base = (char *)toc;
187 iov[1].iov_len = (hdr.nblocks + 1) * sizeof(*toc);
188 offset = iov[0].iov_len + iov[1].iov_len;
190 /* Reserve space for header */
191 lseek(fdw, offset, SEEK_SET);
194 fprintf(stderr, "data size %ju bytes, number of clusters "
195 "%u, index length %zu bytes\n", sb.st_size,
196 hdr.nblocks, iov[1].iov_len);
198 /* Init lzma encoder */
199 if (lzma_lzma_preset(&opt_lzma, LZMA_PRESET_DEFAULT))
200 errx(1, "Error loading LZMA preset");
202 filters[0].id = LZMA_FILTER_LZMA2;
203 filters[0].options = &opt_lzma;
204 filters[1].id = LZMA_VLI_UNKNOWN;
206 for(i = 0; i == 0 || ibuf != NULL; i++) {
207 ibuf = readblock(fdr, ibuf, hdr.blksz);
209 destlen = hdr.blksz*2;
211 ret = lzma_stream_encoder(&strm, filters,
213 if (ret != LZMA_OK) {
214 if (ret == LZMA_MEMLIMIT_ERROR)
215 errx(1, "can't compress data: "
216 "LZMA_MEMLIMIT_ERROR");
218 errx(1, "can't compress data: "
219 "LZMA compressor ERROR");
223 strm.avail_in = hdr.blksz;
224 strm.next_out = obuf;
225 strm.avail_out = hdr.blksz*2;
227 ret = lzma_code(&strm, LZMA_FINISH);
229 if (ret != LZMA_STREAM_END) {
231 errx(1, "lzma_code FINISH failed, code=%d, "
232 "pos(in=%zd, out=%zd)",
234 (hdr.blksz - strm.avail_in),
235 (hdr.blksz*2 - strm.avail_out));
238 destlen -= strm.avail_out;
243 fprintf(stderr, "cluster #%d, in %u bytes, "
244 "out %u bytes\n", i, hdr.blksz, destlen);
246 destlen = USED_BLOCKSIZE - (offset % USED_BLOCKSIZE);
247 memset(obuf, 0, destlen);
249 fprintf(stderr, "padding data with %u bytes"
250 " so that file size is multiple of %d\n",
254 if (write(fdw, obuf, destlen) < 0) {
255 err(1, "write(%s)", oname);
258 toc[i] = htobe64(offset);
264 fprintf(stderr, "compressed data to %ju bytes, saved %lld "
265 "bytes, %.2f%% decrease.\n", offset,
266 (long long)(sb.st_size - offset),
267 100.0 * (long long)(sb.st_size - offset) /
270 /* Convert to big endian */
271 hdr.blksz = htonl(hdr.blksz);
272 hdr.nblocks = htonl(hdr.nblocks);
273 /* Write headers into pre-allocated space */
274 lseek(fdw, 0, SEEK_SET);
275 if (writev(fdw, iov, 2) < 0) {
276 err(1, "writev(%s)", oname);
286 readblock(int fd, char *ibuf, u_int32_t clstsize)
290 bzero(ibuf, clstsize);
291 numread = read(fd, ibuf, clstsize);
293 err(1, "read() failed");
306 fprintf(stderr, "usage: mkulzma [-v] [-o outfile] [-s cluster_size] "
312 safe_malloc(size_t size)
316 retval = malloc(size);
317 if (retval == NULL) {
318 err(1, "can't allocate memory");
328 if (cleanfile != NULL)