2 * Copyright (c) 2003-2010 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 ("??");
90 archive_handle_type_name(unsigned m
)
93 case ARCHIVE_WRITE_MAGIC
: return ("archive_write");
94 case ARCHIVE_READ_MAGIC
: return ("archive_read");
95 case ARCHIVE_WRITE_DISK_MAGIC
: return ("archive_write_disk");
96 case ARCHIVE_READ_DISK_MAGIC
: return ("archive_read_disk");
97 case ARCHIVE_MATCH_MAGIC
: return ("archive_match");
104 write_all_states(char *buff
, unsigned int states
)
110 /* A trick for computing the lowest set bit. */
111 while ((lowbit
= states
& (1 + ~states
)) != 0) {
112 states
&= ~lowbit
; /* Clear the low bit. */
113 strcat(buff
, state_name(lowbit
));
121 * Check magic value and current state.
122 * Magic value mismatches are fatal and result in calls to abort().
123 * State mismatches return ARCHIVE_FATAL.
124 * Otherwise, returns ARCHIVE_OK.
126 * This is designed to catch serious programming errors that violate
127 * the libarchive API.
130 __archive_check_magic(struct archive
*a
, unsigned int magic
,
131 unsigned int state
, const char *function
)
135 const char *handle_type
;
138 * If this isn't some form of archive handle,
139 * then the library user has screwed up so bad that
140 * we don't even have a reliable way to report an error.
142 handle_type
= archive_handle_type_name(a
->magic
);
145 errmsg("PROGRAMMER ERROR: Function ");
147 errmsg(" invoked with invalid archive handle.\n");
151 if (a
->magic
!= magic
) {
152 archive_set_error(a
, -1,
153 "PROGRAMMER ERROR: Function '%s' invoked"
154 " on '%s' archive object, which is not supported.",
157 a
->state
= ARCHIVE_STATE_FATAL
;
158 return (ARCHIVE_FATAL
);
161 if ((a
->state
& state
) == 0) {
162 /* If we're already FATAL, don't overwrite the error. */
163 if (a
->state
!= ARCHIVE_STATE_FATAL
)
164 archive_set_error(a
, -1,
165 "INTERNAL ERROR: Function '%s' invoked with"
166 " archive structure in state '%s',"
167 " should be in state '%s'",
169 write_all_states(states1
, a
->state
),
170 write_all_states(states2
, state
));
171 a
->state
= ARCHIVE_STATE_FATAL
;
172 return (ARCHIVE_FATAL
);