common.h revision 1.4 1 1.1 riastrad /* $NetBSD: common.h,v 1.4 2014/01/22 06:15:57 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.1 riastrad * We require:
87 1.1 riastrad *
88 1.1 riastrad * 0 < blocksize (duh)
89 1.1 riastrad * blocksize % DEV_BSIZE == 0 (for kernel use)
90 1.1 riastrad *
91 1.1 riastrad * blocksize <= UINT32_MAX (per the format)
92 1.1 riastrad * blocksize*2 <= SIZE_MAX (for a compression buffer)
93 1.1 riastrad * blocksize*2 <= ULONG_MAX (for zlib API)
94 1.1 riastrad */
95 1.1 riastrad #define MIN_BLOCKSIZE DEV_BSIZE
96 1.1 riastrad #define DEF_BLOCKSIZE 0x10000
97 1.1 riastrad #define MAX_BLOCKSIZE \
98 1.1 riastrad rounddown(MIN(UINT32_MAX, (MIN(SIZE_MAX, ULONG_MAX) / 2)), \
99 1.1 riastrad MIN_BLOCKSIZE)
100 1.1 riastrad
101 1.1 riastrad /*
102 1.1 riastrad * We require:
103 1.1 riastrad *
104 1.1 riastrad * n_offsets * sizeof(uint64_t) <= SIZE_MAX (array of 64-bit offsets)
105 1.1 riastrad * n_blocks = (n_offsets + 1) (extra offset to set last block end)
106 1.1 riastrad * n_blocks <= UINT32_MAX (per the format)
107 1.1 riastrad * n_offsets <= UINT32_MAX (for the sake of simplicity)
108 1.1 riastrad */
109 1.1 riastrad #define MAX_N_BLOCKS \
110 1.1 riastrad (MIN(UINT32_MAX, (SIZE_MAX / sizeof(uint64_t))) - 1)
111 1.1 riastrad #define MAX_N_OFFSETS (MAX_N_BLOCKS + 1)
112 1.2 riastrad #define MAX_WINDOW_SIZE MAX_N_OFFSETS
113 1.1 riastrad
114 1.1 riastrad struct cloop2_header {
115 1.1 riastrad char cl2h_magic[128];
116 1.1 riastrad uint32_t cl2h_blocksize;
117 1.1 riastrad uint32_t cl2h_n_blocks;
118 1.1 riastrad } __packed;
119 1.1 riastrad
120 1.1 riastrad #define CLOOP2_OFFSET_TABLE_OFFSET 136
121 1.1 riastrad
122 1.1 riastrad struct options {
123 1.1 riastrad int flags;
124 1.3 riastrad #define FLAG_c 0x0001 /* Compress */
125 1.3 riastrad #define FLAG_d 0x0002 /* Decompress */
126 1.3 riastrad #define FLAG_p 0x0004 /* Partial */
127 1.3 riastrad #define FLAG_r 0x0008 /* Restart */
128 1.3 riastrad #define FLAG_s 0x0010 /* block Size */
129 1.3 riastrad #define FLAG_R 0x0020 /* abort on Restart failure */
130 1.3 riastrad #define FLAG_k 0x0040 /* checKpoint blocks */
131 1.3 riastrad #define FLAG_l 0x0080 /* Length of input */
132 1.4 riastrad #define FLAG_w 0x0100 /* Window size */
133 1.1 riastrad uint32_t blocksize;
134 1.1 riastrad uint32_t end_block; /* end for partial transfer */
135 1.1 riastrad uint32_t checkpoint_blocks; /* blocks before checkpoint */
136 1.1 riastrad uint64_t length; /* length of image in bytes */
137 1.4 riastrad uint32_t window_size; /* size of window into offset table */
138 1.1 riastrad };
139 1.1 riastrad
140 1.1 riastrad int vndcompress(int, char **, const struct options *);
141 1.1 riastrad int vnduncompress(int, char **, const struct options *);
142 1.1 riastrad void usage(void) __dead;
143 1.1 riastrad
144 1.1 riastrad static const char cloop2_magic[] = "\
145 1.1 riastrad #!/bin/sh\n\
146 1.1 riastrad #V2.0 Format\n\
147 1.1 riastrad insmod cloop.o file=$0 && mount -r -t iso9660 /dev/cloop $1\n\
148 1.1 riastrad exit $?\n\
149 1.1 riastrad ";
150 1.1 riastrad
151 1.1 riastrad #endif /* VNDCOMPRESS_COMMON_H */
152