1 1.8 riastrad /* $NetBSD: common.h,v 1.8 2017/07/29 21:04:07 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /*- 4 1.1 riastrad * Copyright (c) 2013 The NetBSD Foundation, Inc. 5 1.1 riastrad * All rights reserved. 6 1.1 riastrad * 7 1.1 riastrad * This code is derived from software contributed to The NetBSD Foundation 8 1.1 riastrad * by Taylor R. Campbell. 9 1.1 riastrad * 10 1.1 riastrad * Redistribution and use in source and binary forms, with or without 11 1.1 riastrad * modification, are permitted provided that the following conditions 12 1.1 riastrad * are met: 13 1.1 riastrad * 1. Redistributions of source code must retain the above copyright 14 1.1 riastrad * notice, this list of conditions and the following disclaimer. 15 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 riastrad * notice, this list of conditions and the following disclaimer in the 17 1.1 riastrad * documentation and/or other materials provided with the distribution. 18 1.1 riastrad * 19 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE. 30 1.1 riastrad */ 31 1.1 riastrad 32 1.1 riastrad #ifndef VNDCOMPRESS_COMMON_H 33 1.1 riastrad #define VNDCOMPRESS_COMMON_H 34 1.1 riastrad 35 1.1 riastrad #include <sys/cdefs.h> 36 1.1 riastrad #include <sys/param.h> 37 1.1 riastrad 38 1.1 riastrad #include <limits.h> 39 1.1 riastrad #include <stdint.h> 40 1.1 riastrad 41 1.1 riastrad #ifndef __type_fit 42 1.1 riastrad 43 1.1 riastrad /* XXX Cruft from HEAD. */ 44 1.1 riastrad 45 1.1 riastrad #define __type_mask(t) (/*LINTED*/sizeof(t) < sizeof(intmax_t) ? \ 46 1.1 riastrad (~((1ULL << (sizeof(t) * NBBY)) - 1)) : 0ULL) 47 1.1 riastrad 48 1.1 riastrad #define __zeroll() (0LL) 49 1.1 riastrad #define __negative_p(x) ((x) < 0) 50 1.1 riastrad 51 1.1 riastrad #define __type_min_s(t) ((t)((1ULL << (sizeof(t) * NBBY - 1)))) 52 1.1 riastrad #define __type_max_s(t) ((t)~((1ULL << (sizeof(t) * NBBY - 1)))) 53 1.1 riastrad #define __type_min_u(t) ((t)0ULL) 54 1.1 riastrad #define __type_max_u(t) ((t)~0ULL) 55 1.1 riastrad #define __type_is_signed(t) (/*LINTED*/__type_min_s(t) + (t)1 < (t)1) 56 1.1 riastrad #define __type_min(t) (__type_is_signed(t) ? __type_min_s(t) : __type_min_u(t)) 57 1.1 riastrad #define __type_max(t) (__type_is_signed(t) ? __type_max_s(t) : __type_max_u(t)) 58 1.1 riastrad 59 1.1 riastrad 60 1.1 riastrad #define __type_fit_u(t, a) (/*LINTED*/sizeof(t) < sizeof(intmax_t) ? \ 61 1.1 riastrad (((a) & __type_mask(t)) == 0) : !__negative_p(a)) 62 1.1 riastrad 63 1.1 riastrad #define __type_fit_s(t, a) (/*LINTED*/__negative_p(a) ? \ 64 1.1 riastrad ((intmax_t)((a) + __zeroll()) >= (intmax_t)__type_min_s(t)) : \ 65 1.1 riastrad ((intmax_t)((a) + __zeroll()) <= (intmax_t)__type_max_s(t))) 66 1.1 riastrad 67 1.1 riastrad /* 68 1.1 riastrad * return true if value 'a' fits in type 't' 69 1.1 riastrad */ 70 1.1 riastrad #define __type_fit(t, a) (__type_is_signed(t) ? \ 71 1.1 riastrad __type_fit_s(t, a) : __type_fit_u(t, a)) 72 1.1 riastrad 73 1.1 riastrad #endif 74 1.1 riastrad 75 1.1 riastrad /* XXX urk */ 76 1.1 riastrad #ifndef OFF_MAX 77 1.1 riastrad #define OFF_MAX __type_max(off_t) 78 1.1 riastrad #endif 79 1.1 riastrad 80 1.1 riastrad /* XXX Why is this kernel-only? */ 81 1.1 riastrad #define SET(t, f) ((t) |= (f)) 82 1.1 riastrad #define CLR(t, f) ((t) &= ~(f)) 83 1.1 riastrad #define ISSET(t, f) ((t) & (f)) 84 1.1 riastrad 85 1.1 riastrad /* 86 1.8 riastrad * Bounds checks for arithmetic. 87 1.8 riastrad */ 88 1.8 riastrad #define ADD_OK(T, A, B) ((A) <= __type_max(T) - (B)) 89 1.8 riastrad 90 1.8 riastrad #define MUL_OK(T, A, B) ((A) <= __type_max(T)/(B)) 91 1.8 riastrad 92 1.8 riastrad #define HOWMANY(X, N) (((X) + ((N) - 1))/(N)) 93 1.8 riastrad #define TOOMANY(T, X, N, M) (!ADD_OK(T, X, (N) - 1) || HOWMANY(X, N) > (M)) 94 1.8 riastrad 95 1.8 riastrad /* 96 1.1 riastrad * We require: 97 1.1 riastrad * 98 1.1 riastrad * 0 < blocksize (duh) 99 1.1 riastrad * blocksize % DEV_BSIZE == 0 (for kernel use) 100 1.1 riastrad * 101 1.1 riastrad * blocksize <= UINT32_MAX (per the format) 102 1.1 riastrad * blocksize*2 <= SIZE_MAX (for a compression buffer) 103 1.1 riastrad * blocksize*2 <= ULONG_MAX (for zlib API) 104 1.1 riastrad */ 105 1.1 riastrad #define MIN_BLOCKSIZE DEV_BSIZE 106 1.1 riastrad #define DEF_BLOCKSIZE 0x10000 107 1.1 riastrad #define MAX_BLOCKSIZE \ 108 1.1 riastrad rounddown(MIN(UINT32_MAX, (MIN(SIZE_MAX, ULONG_MAX) / 2)), \ 109 1.1 riastrad MIN_BLOCKSIZE) 110 1.1 riastrad 111 1.1 riastrad /* 112 1.1 riastrad * We require: 113 1.1 riastrad * 114 1.1 riastrad * n_offsets * sizeof(uint64_t) <= SIZE_MAX (array of 64-bit offsets) 115 1.1 riastrad * n_blocks = (n_offsets + 1) (extra offset to set last block end) 116 1.1 riastrad * n_blocks <= UINT32_MAX (per the format) 117 1.1 riastrad * n_offsets <= UINT32_MAX (for the sake of simplicity) 118 1.1 riastrad */ 119 1.1 riastrad #define MAX_N_BLOCKS \ 120 1.1 riastrad (MIN(UINT32_MAX, (SIZE_MAX / sizeof(uint64_t))) - 1) 121 1.1 riastrad #define MAX_N_OFFSETS (MAX_N_BLOCKS + 1) 122 1.6 riastrad 123 1.6 riastrad /* 124 1.7 riastrad * The window size is at most the number of offsets, or the largest 125 1.7 riastrad * uint32_t value. The default window size is chosen so that windows 126 1.6 riastrad * fit in one 4096-byte page of memory. We could use 64k bytes, or 127 1.6 riastrad * st_blksize, to maximize I/O transfer size, but the transfers won't 128 1.6 riastrad * be aligned without a lot of extra work. 129 1.6 riastrad */ 130 1.7 riastrad #define MAX_WINDOW_SIZE MIN(UINT32_MAX, MAX_N_OFFSETS) 131 1.6 riastrad #define DEF_WINDOW_SIZE 512 132 1.1 riastrad 133 1.1 riastrad struct cloop2_header { 134 1.1 riastrad char cl2h_magic[128]; 135 1.1 riastrad uint32_t cl2h_blocksize; 136 1.1 riastrad uint32_t cl2h_n_blocks; 137 1.1 riastrad } __packed; 138 1.1 riastrad 139 1.1 riastrad #define CLOOP2_OFFSET_TABLE_OFFSET 136 140 1.1 riastrad 141 1.1 riastrad struct options { 142 1.1 riastrad int flags; 143 1.3 riastrad #define FLAG_c 0x0001 /* Compress */ 144 1.3 riastrad #define FLAG_d 0x0002 /* Decompress */ 145 1.3 riastrad #define FLAG_p 0x0004 /* Partial */ 146 1.3 riastrad #define FLAG_r 0x0008 /* Restart */ 147 1.5 riastrad #define FLAG_b 0x0010 /* Block size */ 148 1.3 riastrad #define FLAG_R 0x0020 /* abort on Restart failure */ 149 1.3 riastrad #define FLAG_k 0x0040 /* checKpoint blocks */ 150 1.3 riastrad #define FLAG_l 0x0080 /* Length of input */ 151 1.4 riastrad #define FLAG_w 0x0100 /* Window size */ 152 1.1 riastrad uint32_t blocksize; 153 1.1 riastrad uint32_t end_block; /* end for partial transfer */ 154 1.1 riastrad uint32_t checkpoint_blocks; /* blocks before checkpoint */ 155 1.1 riastrad uint64_t length; /* length of image in bytes */ 156 1.4 riastrad uint32_t window_size; /* size of window into offset table */ 157 1.1 riastrad }; 158 1.1 riastrad 159 1.1 riastrad int vndcompress(int, char **, const struct options *); 160 1.1 riastrad int vnduncompress(int, char **, const struct options *); 161 1.1 riastrad void usage(void) __dead; 162 1.1 riastrad 163 1.1 riastrad static const char cloop2_magic[] = "\ 164 1.1 riastrad #!/bin/sh\n\ 165 1.1 riastrad #V2.0 Format\n\ 166 1.1 riastrad insmod cloop.o file=$0 && mount -r -t iso9660 /dev/cloop $1\n\ 167 1.1 riastrad exit $?\n\ 168 1.1 riastrad "; 169 1.1 riastrad 170 1.1 riastrad #endif /* VNDCOMPRESS_COMMON_H */ 171