2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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: head/lib/libarchive/archive_check_magic.c 201089 2009-12-28 02:20:23Z kientzle $");
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
43 #if defined(_WIN32) && !defined(__CYGWIN__)
48 #include "archive_private.h"
57 written = write(2, m, strlen(m));
68 #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
69 /* Cause a breakpoint exception */
72 abort(); /* Terminate the program abnormally. */
76 state_name(unsigned s)
79 case ARCHIVE_STATE_NEW: return ("new");
80 case ARCHIVE_STATE_HEADER: return ("header");
81 case ARCHIVE_STATE_DATA: return ("data");
82 case ARCHIVE_STATE_EOF: return ("eof");
83 case ARCHIVE_STATE_CLOSED: return ("closed");
84 case ARCHIVE_STATE_FATAL: return ("fatal");
85 default: return ("??");
91 write_all_states(unsigned int states)
95 /* A trick for computing the lowest set bit. */
96 while ((lowbit = states & (1 + ~states)) != 0) {
97 states &= ~lowbit; /* Clear the low bit. */
98 errmsg(state_name(lowbit));
105 * Check magic value and current state; bail if it isn't valid.
107 * This is designed to catch serious programming errors that violate
108 * the libarchive API.
111 __archive_check_magic(struct archive *a, unsigned int magic,
112 unsigned int state, const char *function)
114 if (a->magic != magic) {
115 errmsg("INTERNAL ERROR: Function ");
117 errmsg(" invoked with invalid struct archive structure.\n");
121 if (state == ARCHIVE_STATE_ANY)
124 if ((a->state & state) == 0) {
125 errmsg("INTERNAL ERROR: Function '");
127 errmsg("' invoked with archive structure in state '");
128 write_all_states(a->state);
129 errmsg("', should be in state '");
130 write_all_states(state);