1*76d4b812Sriastradh /* $NetBSD: common.h,v 1.8 2017/07/29 21:04:07 riastradh Exp $ */ 285bbc49aSriastradh 385bbc49aSriastradh /*- 485bbc49aSriastradh * Copyright (c) 2013 The NetBSD Foundation, Inc. 585bbc49aSriastradh * All rights reserved. 685bbc49aSriastradh * 785bbc49aSriastradh * This code is derived from software contributed to The NetBSD Foundation 885bbc49aSriastradh * by Taylor R. Campbell. 985bbc49aSriastradh * 1085bbc49aSriastradh * Redistribution and use in source and binary forms, with or without 1185bbc49aSriastradh * modification, are permitted provided that the following conditions 1285bbc49aSriastradh * are met: 1385bbc49aSriastradh * 1. Redistributions of source code must retain the above copyright 1485bbc49aSriastradh * notice, this list of conditions and the following disclaimer. 1585bbc49aSriastradh * 2. Redistributions in binary form must reproduce the above copyright 1685bbc49aSriastradh * notice, this list of conditions and the following disclaimer in the 1785bbc49aSriastradh * documentation and/or other materials provided with the distribution. 1885bbc49aSriastradh * 1985bbc49aSriastradh * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 2085bbc49aSriastradh * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 2185bbc49aSriastradh * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2285bbc49aSriastradh * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 2385bbc49aSriastradh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2485bbc49aSriastradh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2585bbc49aSriastradh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2685bbc49aSriastradh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2785bbc49aSriastradh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2885bbc49aSriastradh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2985bbc49aSriastradh * POSSIBILITY OF SUCH DAMAGE. 3085bbc49aSriastradh */ 3185bbc49aSriastradh 3285bbc49aSriastradh #ifndef VNDCOMPRESS_COMMON_H 3385bbc49aSriastradh #define VNDCOMPRESS_COMMON_H 3485bbc49aSriastradh 3585bbc49aSriastradh #include <sys/cdefs.h> 3685bbc49aSriastradh #include <sys/param.h> 3785bbc49aSriastradh 3885bbc49aSriastradh #include <limits.h> 3985bbc49aSriastradh #include <stdint.h> 4085bbc49aSriastradh 4185bbc49aSriastradh #ifndef __type_fit 4285bbc49aSriastradh 4385bbc49aSriastradh /* XXX Cruft from HEAD. */ 4485bbc49aSriastradh 4585bbc49aSriastradh #define __type_mask(t) (/*LINTED*/sizeof(t) < sizeof(intmax_t) ? \ 4685bbc49aSriastradh (~((1ULL << (sizeof(t) * NBBY)) - 1)) : 0ULL) 4785bbc49aSriastradh 4885bbc49aSriastradh #define __zeroll() (0LL) 4985bbc49aSriastradh #define __negative_p(x) ((x) < 0) 5085bbc49aSriastradh 5185bbc49aSriastradh #define __type_min_s(t) ((t)((1ULL << (sizeof(t) * NBBY - 1)))) 5285bbc49aSriastradh #define __type_max_s(t) ((t)~((1ULL << (sizeof(t) * NBBY - 1)))) 5385bbc49aSriastradh #define __type_min_u(t) ((t)0ULL) 5485bbc49aSriastradh #define __type_max_u(t) ((t)~0ULL) 5585bbc49aSriastradh #define __type_is_signed(t) (/*LINTED*/__type_min_s(t) + (t)1 < (t)1) 5685bbc49aSriastradh #define __type_min(t) (__type_is_signed(t) ? __type_min_s(t) : __type_min_u(t)) 5785bbc49aSriastradh #define __type_max(t) (__type_is_signed(t) ? __type_max_s(t) : __type_max_u(t)) 5885bbc49aSriastradh 5985bbc49aSriastradh 6085bbc49aSriastradh #define __type_fit_u(t, a) (/*LINTED*/sizeof(t) < sizeof(intmax_t) ? \ 6185bbc49aSriastradh (((a) & __type_mask(t)) == 0) : !__negative_p(a)) 6285bbc49aSriastradh 6385bbc49aSriastradh #define __type_fit_s(t, a) (/*LINTED*/__negative_p(a) ? \ 6485bbc49aSriastradh ((intmax_t)((a) + __zeroll()) >= (intmax_t)__type_min_s(t)) : \ 6585bbc49aSriastradh ((intmax_t)((a) + __zeroll()) <= (intmax_t)__type_max_s(t))) 6685bbc49aSriastradh 6785bbc49aSriastradh /* 6885bbc49aSriastradh * return true if value 'a' fits in type 't' 6985bbc49aSriastradh */ 7085bbc49aSriastradh #define __type_fit(t, a) (__type_is_signed(t) ? \ 7185bbc49aSriastradh __type_fit_s(t, a) : __type_fit_u(t, a)) 7285bbc49aSriastradh 7385bbc49aSriastradh #endif 7485bbc49aSriastradh 7585bbc49aSriastradh /* XXX urk */ 7685bbc49aSriastradh #ifndef OFF_MAX 7785bbc49aSriastradh #define OFF_MAX __type_max(off_t) 7885bbc49aSriastradh #endif 7985bbc49aSriastradh 8085bbc49aSriastradh /* XXX Why is this kernel-only? */ 8185bbc49aSriastradh #define SET(t, f) ((t) |= (f)) 8285bbc49aSriastradh #define CLR(t, f) ((t) &= ~(f)) 8385bbc49aSriastradh #define ISSET(t, f) ((t) & (f)) 8485bbc49aSriastradh 8585bbc49aSriastradh /* 86*76d4b812Sriastradh * Bounds checks for arithmetic. 87*76d4b812Sriastradh */ 88*76d4b812Sriastradh #define ADD_OK(T, A, B) ((A) <= __type_max(T) - (B)) 89*76d4b812Sriastradh 90*76d4b812Sriastradh #define MUL_OK(T, A, B) ((A) <= __type_max(T)/(B)) 91*76d4b812Sriastradh 92*76d4b812Sriastradh #define HOWMANY(X, N) (((X) + ((N) - 1))/(N)) 93*76d4b812Sriastradh #define TOOMANY(T, X, N, M) (!ADD_OK(T, X, (N) - 1) || HOWMANY(X, N) > (M)) 94*76d4b812Sriastradh 95*76d4b812Sriastradh /* 9685bbc49aSriastradh * We require: 9785bbc49aSriastradh * 9885bbc49aSriastradh * 0 < blocksize (duh) 9985bbc49aSriastradh * blocksize % DEV_BSIZE == 0 (for kernel use) 10085bbc49aSriastradh * 10185bbc49aSriastradh * blocksize <= UINT32_MAX (per the format) 10285bbc49aSriastradh * blocksize*2 <= SIZE_MAX (for a compression buffer) 10385bbc49aSriastradh * blocksize*2 <= ULONG_MAX (for zlib API) 10485bbc49aSriastradh */ 10585bbc49aSriastradh #define MIN_BLOCKSIZE DEV_BSIZE 10685bbc49aSriastradh #define DEF_BLOCKSIZE 0x10000 10785bbc49aSriastradh #define MAX_BLOCKSIZE \ 10885bbc49aSriastradh rounddown(MIN(UINT32_MAX, (MIN(SIZE_MAX, ULONG_MAX) / 2)), \ 10985bbc49aSriastradh MIN_BLOCKSIZE) 11085bbc49aSriastradh 11185bbc49aSriastradh /* 11285bbc49aSriastradh * We require: 11385bbc49aSriastradh * 11485bbc49aSriastradh * n_offsets * sizeof(uint64_t) <= SIZE_MAX (array of 64-bit offsets) 11585bbc49aSriastradh * n_blocks = (n_offsets + 1) (extra offset to set last block end) 11685bbc49aSriastradh * n_blocks <= UINT32_MAX (per the format) 11785bbc49aSriastradh * n_offsets <= UINT32_MAX (for the sake of simplicity) 11885bbc49aSriastradh */ 11985bbc49aSriastradh #define MAX_N_BLOCKS \ 12085bbc49aSriastradh (MIN(UINT32_MAX, (SIZE_MAX / sizeof(uint64_t))) - 1) 12185bbc49aSriastradh #define MAX_N_OFFSETS (MAX_N_BLOCKS + 1) 122f13ecd80Sriastradh 123f13ecd80Sriastradh /* 1249b8e93bfSriastradh * The window size is at most the number of offsets, or the largest 1259b8e93bfSriastradh * uint32_t value. The default window size is chosen so that windows 126f13ecd80Sriastradh * fit in one 4096-byte page of memory. We could use 64k bytes, or 127f13ecd80Sriastradh * st_blksize, to maximize I/O transfer size, but the transfers won't 128f13ecd80Sriastradh * be aligned without a lot of extra work. 129f13ecd80Sriastradh */ 1309b8e93bfSriastradh #define MAX_WINDOW_SIZE MIN(UINT32_MAX, MAX_N_OFFSETS) 131f13ecd80Sriastradh #define DEF_WINDOW_SIZE 512 13285bbc49aSriastradh 13385bbc49aSriastradh struct cloop2_header { 13485bbc49aSriastradh char cl2h_magic[128]; 13585bbc49aSriastradh uint32_t cl2h_blocksize; 13685bbc49aSriastradh uint32_t cl2h_n_blocks; 13785bbc49aSriastradh } __packed; 13885bbc49aSriastradh 13985bbc49aSriastradh #define CLOOP2_OFFSET_TABLE_OFFSET 136 14085bbc49aSriastradh 14185bbc49aSriastradh struct options { 14285bbc49aSriastradh int flags; 1431c2cd5c0Sriastradh #define FLAG_c 0x0001 /* Compress */ 1441c2cd5c0Sriastradh #define FLAG_d 0x0002 /* Decompress */ 1451c2cd5c0Sriastradh #define FLAG_p 0x0004 /* Partial */ 1461c2cd5c0Sriastradh #define FLAG_r 0x0008 /* Restart */ 1479aa3cfafSriastradh #define FLAG_b 0x0010 /* Block size */ 1481c2cd5c0Sriastradh #define FLAG_R 0x0020 /* abort on Restart failure */ 1491c2cd5c0Sriastradh #define FLAG_k 0x0040 /* checKpoint blocks */ 1501c2cd5c0Sriastradh #define FLAG_l 0x0080 /* Length of input */ 1517c5bfcbeSriastradh #define FLAG_w 0x0100 /* Window size */ 15285bbc49aSriastradh uint32_t blocksize; 15385bbc49aSriastradh uint32_t end_block; /* end for partial transfer */ 15485bbc49aSriastradh uint32_t checkpoint_blocks; /* blocks before checkpoint */ 15585bbc49aSriastradh uint64_t length; /* length of image in bytes */ 1567c5bfcbeSriastradh uint32_t window_size; /* size of window into offset table */ 15785bbc49aSriastradh }; 15885bbc49aSriastradh 15985bbc49aSriastradh int vndcompress(int, char **, const struct options *); 16085bbc49aSriastradh int vnduncompress(int, char **, const struct options *); 16185bbc49aSriastradh void usage(void) __dead; 16285bbc49aSriastradh 16385bbc49aSriastradh static const char cloop2_magic[] = "\ 16485bbc49aSriastradh #!/bin/sh\n\ 16585bbc49aSriastradh #V2.0 Format\n\ 16685bbc49aSriastradh insmod cloop.o file=$0 && mount -r -t iso9660 /dev/cloop $1\n\ 16785bbc49aSriastradh exit $?\n\ 16885bbc49aSriastradh "; 16985bbc49aSriastradh 17085bbc49aSriastradh #endif /* VNDCOMPRESS_COMMON_H */ 171