Oops, add missing space.
[dragonfly/port-amd64.git] / contrib / libarchive-2 / libarchive / archive_check_magic.c
blob715486dcfeb762c54ec13a85082c5afd7c3de948
1 /*-
2 * Copyright (c) 2003-2007 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 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD: src/lib/libarchive/archive_check_magic.c,v 1.8 2007/04/02 00:15:45 kientzle Exp $");
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
31 #endif
33 #include <stdio.h>
34 #ifdef HAVE_STDLIB_H
35 #include <stdlib.h>
36 #endif
37 #ifdef HAVE_STRING_H
38 #include <string.h>
39 #endif
40 #ifdef HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
44 #include "archive_private.h"
46 static void
47 errmsg(const char *m)
49 write(STDERR_FILENO, m, strlen(m));
52 static void
53 diediedie(void)
55 *(char *)0 = 1; /* Deliberately segfault and force a coredump. */
56 _exit(1); /* If that didn't work, just exit with an error. */
59 static const char *
60 state_name(unsigned s)
62 switch (s) {
63 case ARCHIVE_STATE_NEW: return ("new");
64 case ARCHIVE_STATE_HEADER: return ("header");
65 case ARCHIVE_STATE_DATA: return ("data");
66 case ARCHIVE_STATE_EOF: return ("eof");
67 case ARCHIVE_STATE_CLOSED: return ("closed");
68 case ARCHIVE_STATE_FATAL: return ("fatal");
69 default: return ("??");
74 static void
75 write_all_states(unsigned int states)
77 unsigned int lowbit;
79 /* A trick for computing the lowest set bit. */
80 while ((lowbit = states & (-states)) != 0) {
81 states &= ~lowbit; /* Clear the low bit. */
82 errmsg(state_name(lowbit));
83 if (states != 0)
84 errmsg("/");
89 * Check magic value and current state; bail if it isn't valid.
91 * This is designed to catch serious programming errors that violate
92 * the libarchive API.
94 void
95 __archive_check_magic(struct archive *a, unsigned int magic,
96 unsigned int state, const char *function)
98 if (a->magic != magic) {
99 errmsg("INTERNAL ERROR: Function ");
100 errmsg(function);
101 errmsg(" invoked with invalid struct archive structure.\n");
102 diediedie();
105 if (state == ARCHIVE_STATE_ANY)
106 return;
108 if ((a->state & state) == 0) {
109 errmsg("INTERNAL ERROR: Function '");
110 errmsg(function);
111 errmsg("' invoked with archive structure in state '");
112 write_all_states(a->state);
113 errmsg("', should be in state '");
114 write_all_states(state);
115 errmsg("'\n");
116 diediedie();