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