hier.7: Document some recently added directories.
[dragonfly.git] / contrib / libarchive / libarchive / archive_pack_dev.c
blob6b7b4726d6eab5246f54240186a651bd449f0d49
1 /* $NetBSD: pack_dev.c,v 1.12 2013/06/14 16:28:20 tsutsui Exp $ */
3 /*-
4 * Copyright (c) 1998, 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 /* Originally from NetBSD's mknod(8) source. */
34 #include "archive_platform.h"
36 #if HAVE_SYS_CDEFS_H
37 #include <sys/cdefs.h>
38 #endif
39 #if !defined(lint)
40 __RCSID("$NetBSD$");
41 #endif /* not lint */
43 #ifdef HAVE_LIMITS_H
44 #include <limits.h>
45 #endif
47 #include <stdio.h>
48 #ifdef HAVE_STDLIB_H
49 #include <stdlib.h>
50 #endif
51 #ifdef HAVE_STRING_H
52 #include <string.h>
53 #endif
54 #ifdef HAVE_SYS_TYPES_H
55 #include <sys/types.h>
56 #endif
57 #ifdef HAVE_SYS_STAT_H
58 #include <sys/stat.h>
59 #endif
60 #ifdef HAVE_UNISTD_H
61 #include <unistd.h>
62 #endif
64 #include "archive_pack_dev.h"
66 static pack_t pack_netbsd;
67 static pack_t pack_freebsd;
68 static pack_t pack_8_8;
69 static pack_t pack_12_20;
70 static pack_t pack_14_18;
71 static pack_t pack_8_24;
72 static pack_t pack_bsdos;
73 static int compare_format(const void *, const void *);
75 static const char iMajorError[] = "invalid major number";
76 static const char iMinorError[] = "invalid minor number";
77 static const char tooManyFields[] = "too many fields for format";
79 /* This is blatantly stolen from libarchive/archive_entry.c,
80 * in an attempt to get this to play nice on MinGW... */
81 #if !defined(HAVE_MAJOR) && !defined(major)
82 /* Replacement for major/minor/makedev. */
83 #define major(x) ((int)(0x00ff & ((x) >> 8)))
84 #define minor(x) ((int)(0xffff00ff & (x)))
85 #define makedev(maj,min) ((0xff00 & ((maj)<<8)) | (0xffff00ff & (min)))
86 #endif
88 /* Play games to come up with a suitable makedev() definition. */
89 #ifdef __QNXNTO__
90 /* QNX. <sigh> */
91 #include <sys/netmgr.h>
92 #define apd_makedev(maj, min) makedev(ND_LOCAL_NODE, (maj), (min))
93 #elif defined makedev
94 /* There's a "makedev" macro. */
95 #define apd_makedev(maj, min) makedev((maj), (min))
96 #elif defined mkdev || ((defined _WIN32 || defined __WIN32__) && !defined(__CYGWIN__))
97 /* Windows. <sigh> */
98 #define apd_makedev(maj, min) mkdev((maj), (min))
99 #else
100 /* There's a "makedev" function. */
101 #define apd_makedev(maj, min) makedev((maj), (min))
102 #endif
104 /* exported */
105 dev_t
106 pack_native(int n, unsigned long numbers[], const char **error)
108 dev_t dev = 0;
110 if (n == 2) {
111 dev = apd_makedev(numbers[0], numbers[1]);
112 if ((unsigned long)major(dev) != numbers[0])
113 *error = iMajorError;
114 else if ((unsigned long)minor(dev) != numbers[1])
115 *error = iMinorError;
116 } else
117 *error = tooManyFields;
118 return (dev);
122 static dev_t
123 pack_netbsd(int n, unsigned long numbers[], const char **error)
125 dev_t dev = 0;
127 if (n == 2) {
128 dev = makedev_netbsd(numbers[0], numbers[1]);
129 if ((unsigned long)major_netbsd(dev) != numbers[0])
130 *error = iMajorError;
131 else if ((unsigned long)minor_netbsd(dev) != numbers[1])
132 *error = iMinorError;
133 } else
134 *error = tooManyFields;
135 return (dev);
139 #define major_freebsd(x) ((int32_t)(((x) & 0x0000ff00) >> 8))
140 #define minor_freebsd(x) ((int32_t)(((x) & 0xffff00ff) >> 0))
141 #define makedev_freebsd(x,y) ((dev_t)((((x) << 8) & 0x0000ff00) | \
142 (((y) << 0) & 0xffff00ff)))
144 static dev_t
145 pack_freebsd(int n, unsigned long numbers[], const char **error)
147 dev_t dev = 0;
149 if (n == 2) {
150 dev = makedev_freebsd(numbers[0], numbers[1]);
151 if ((unsigned long)major_freebsd(dev) != numbers[0])
152 *error = iMajorError;
153 if ((unsigned long)minor_freebsd(dev) != numbers[1])
154 *error = iMinorError;
155 } else
156 *error = tooManyFields;
157 return (dev);
161 #define major_8_8(x) ((int32_t)(((x) & 0x0000ff00) >> 8))
162 #define minor_8_8(x) ((int32_t)(((x) & 0x000000ff) >> 0))
163 #define makedev_8_8(x,y) ((dev_t)((((x) << 8) & 0x0000ff00) | \
164 (((y) << 0) & 0x000000ff)))
166 static dev_t
167 pack_8_8(int n, unsigned long numbers[], const char **error)
169 dev_t dev = 0;
171 if (n == 2) {
172 dev = makedev_8_8(numbers[0], numbers[1]);
173 if ((unsigned long)major_8_8(dev) != numbers[0])
174 *error = iMajorError;
175 if ((unsigned long)minor_8_8(dev) != numbers[1])
176 *error = iMinorError;
177 } else
178 *error = tooManyFields;
179 return (dev);
183 #define major_12_20(x) ((int32_t)(((x) & 0xfff00000) >> 20))
184 #define minor_12_20(x) ((int32_t)(((x) & 0x000fffff) >> 0))
185 #define makedev_12_20(x,y) ((dev_t)((((x) << 20) & 0xfff00000) | \
186 (((y) << 0) & 0x000fffff)))
188 static dev_t
189 pack_12_20(int n, unsigned long numbers[], const char **error)
191 dev_t dev = 0;
193 if (n == 2) {
194 dev = makedev_12_20(numbers[0], numbers[1]);
195 if ((unsigned long)major_12_20(dev) != numbers[0])
196 *error = iMajorError;
197 if ((unsigned long)minor_12_20(dev) != numbers[1])
198 *error = iMinorError;
199 } else
200 *error = tooManyFields;
201 return (dev);
205 #define major_14_18(x) ((int32_t)(((x) & 0xfffc0000) >> 18))
206 #define minor_14_18(x) ((int32_t)(((x) & 0x0003ffff) >> 0))
207 #define makedev_14_18(x,y) ((dev_t)((((x) << 18) & 0xfffc0000) | \
208 (((y) << 0) & 0x0003ffff)))
210 static dev_t
211 pack_14_18(int n, unsigned long numbers[], const char **error)
213 dev_t dev = 0;
215 if (n == 2) {
216 dev = makedev_14_18(numbers[0], numbers[1]);
217 if ((unsigned long)major_14_18(dev) != numbers[0])
218 *error = iMajorError;
219 if ((unsigned long)minor_14_18(dev) != numbers[1])
220 *error = iMinorError;
221 } else
222 *error = tooManyFields;
223 return (dev);
227 #define major_8_24(x) ((int32_t)(((x) & 0xff000000) >> 24))
228 #define minor_8_24(x) ((int32_t)(((x) & 0x00ffffff) >> 0))
229 #define makedev_8_24(x,y) ((dev_t)((((x) << 24) & 0xff000000) | \
230 (((y) << 0) & 0x00ffffff)))
232 static dev_t
233 pack_8_24(int n, unsigned long numbers[], const char **error)
235 dev_t dev = 0;
237 if (n == 2) {
238 dev = makedev_8_24(numbers[0], numbers[1]);
239 if ((unsigned long)major_8_24(dev) != numbers[0])
240 *error = iMajorError;
241 if ((unsigned long)minor_8_24(dev) != numbers[1])
242 *error = iMinorError;
243 } else
244 *error = tooManyFields;
245 return (dev);
249 #define major_12_12_8(x) ((int32_t)(((x) & 0xfff00000) >> 20))
250 #define unit_12_12_8(x) ((int32_t)(((x) & 0x000fff00) >> 8))
251 #define subunit_12_12_8(x) ((int32_t)(((x) & 0x000000ff) >> 0))
252 #define makedev_12_12_8(x,y,z) ((dev_t)((((x) << 20) & 0xfff00000) | \
253 (((y) << 8) & 0x000fff00) | \
254 (((z) << 0) & 0x000000ff)))
256 static dev_t
257 pack_bsdos(int n, unsigned long numbers[], const char **error)
259 dev_t dev = 0;
261 if (n == 2) {
262 dev = makedev_12_20(numbers[0], numbers[1]);
263 if ((unsigned long)major_12_20(dev) != numbers[0])
264 *error = iMajorError;
265 if ((unsigned long)minor_12_20(dev) != numbers[1])
266 *error = iMinorError;
267 } else if (n == 3) {
268 dev = makedev_12_12_8(numbers[0], numbers[1], numbers[2]);
269 if ((unsigned long)major_12_12_8(dev) != numbers[0])
270 *error = iMajorError;
271 if ((unsigned long)unit_12_12_8(dev) != numbers[1])
272 *error = "invalid unit number";
273 if ((unsigned long)subunit_12_12_8(dev) != numbers[2])
274 *error = "invalid subunit number";
275 } else
276 *error = tooManyFields;
277 return (dev);
281 /* list of formats and pack functions */
282 /* this list must be sorted lexically */
283 static struct format {
284 const char *name;
285 pack_t *pack;
286 } formats[] = {
287 {"386bsd", pack_8_8},
288 {"4bsd", pack_8_8},
289 {"bsdos", pack_bsdos},
290 {"freebsd", pack_freebsd},
291 {"hpux", pack_8_24},
292 {"isc", pack_8_8},
293 {"linux", pack_8_8},
294 {"native", pack_native},
295 {"netbsd", pack_netbsd},
296 {"osf1", pack_12_20},
297 {"sco", pack_8_8},
298 {"solaris", pack_14_18},
299 {"sunos", pack_8_8},
300 {"svr3", pack_8_8},
301 {"svr4", pack_14_18},
302 {"ultrix", pack_8_8},
305 static int
306 compare_format(const void *key, const void *element)
308 const char *name;
309 const struct format *format;
311 name = key;
312 format = element;
314 return (strcmp(name, format->name));
318 pack_t *
319 pack_find(const char *name)
321 struct format *format;
323 format = bsearch(name, formats,
324 sizeof(formats)/sizeof(formats[0]),
325 sizeof(formats[0]), compare_format);
326 if (format == 0)
327 return (NULL);
328 return (format->pack);