Upgrade to libarchive 2.0.25 which gives us a nice speed boost along with
[dragonfly/port-amd64.git] / contrib / libarchive-1.3.1 / libarchive / archive_check_magic.c
blob63932a0bd3d5239e1d0ba8066adb76131d68cf9c
1 /*-
2 * Copyright (c) 2003-2004 Tim Kientzle
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD: src/lib/libarchive/archive_check_magic.c,v 1.5 2004/10/18 04:34:30 kientzle Exp $");
30 #include <sys/types.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <unistd.h>
36 #include "archive_private.h"
38 static void
39 errmsg(const char *m)
41 write(STDERR_FILENO, m, strlen(m));
44 static void
45 diediedie(void)
47 *(char *)0 = 1; /* Deliberately segfault and force a coredump. */
48 _exit(1); /* If that didn't work, just exit with an error. */
51 static const char *
52 state_name(unsigned s)
54 switch (s) {
55 case ARCHIVE_STATE_NEW: return ("new");
56 case ARCHIVE_STATE_HEADER: return ("header");
57 case ARCHIVE_STATE_DATA: return ("data");
58 case ARCHIVE_STATE_EOF: return ("eof");
59 case ARCHIVE_STATE_CLOSED: return ("closed");
60 case ARCHIVE_STATE_FATAL: return ("fatal");
61 default: return ("??");
66 static void
67 write_all_states(int states)
69 unsigned lowbit;
71 /* A trick for computing the lowest set bit. */
72 while ((lowbit = states & (-states)) != 0) {
73 states &= ~lowbit; /* Clear the low bit. */
74 errmsg(state_name(lowbit));
75 if (states != 0)
76 errmsg("/");
81 * Check magic value and current state; bail if it isn't valid.
83 * This is designed to catch serious programming errors that violate
84 * the libarchive API.
86 void
87 __archive_check_magic(struct archive *a, unsigned magic, unsigned state,
88 const char *function)
90 if (a->magic != magic) {
91 errmsg("INTERNAL ERROR: Function ");
92 errmsg(function);
93 errmsg(" invoked with invalid struct archive structure.\n");
94 diediedie();
97 if (state == ARCHIVE_STATE_ANY)
98 return;
100 if ((a->state & state) == 0) {
101 errmsg("INTERNAL ERROR: Function '");
102 errmsg(function);
103 errmsg("' invoked with archive structure in state '");
104 write_all_states(a->state);
105 errmsg("', should be in state '");
106 write_all_states(state);
107 errmsg("'\n");
108 diediedie();