1 /*-
2 * Copyright (c) 2003-2010 Tim Kientzle
3 * All rights reserved.
4 *
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.
13 *
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.
24 */
25
26 #include "archive_platform.h"
27
28 #ifdef HAVE_SYS_TYPES_H
29 #include <sys/types.h>
30 #endif
31
32 #include <stdio.h>
33 #ifdef HAVE_STDLIB_H
34 #include <stdlib.h>
35 #endif
36 #ifdef HAVE_STRING_H
37 #include <string.h>
38 #endif
39 #ifdef HAVE_UNISTD_H
40 #include <unistd.h>
41 #endif
42 #if defined(_WIN32) && !defined(__CYGWIN__)
43 #include <windows.h>
44 #include <winbase.h>
45 #endif
46
47 #include "archive_private.h"
48
49 static void
errmsg(const char * m)50 errmsg(const char *m)
51 {
52 size_t s = strlen(m);
53 ssize_t written;
54
55 while (s > 0) {
56 written = write(2, m, s);
57 if (written <= 0)
58 return;
59 m += written;
60 s -= written;
61 }
62 }
63
64 static __LA_NORETURN void
diediedie(void)65 diediedie(void)
66 {
67 #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
68 /* Cause a breakpoint exception */
69 DebugBreak();
70 #endif
71 abort(); /* Terminate the program abnormally. */
72 }
73
74 static const char *
state_name(unsigned s)75 state_name(unsigned s)
76 {
77 switch (s) {
78 case ARCHIVE_STATE_NEW: return ("new");
79 case ARCHIVE_STATE_HEADER: return ("header");
80 case ARCHIVE_STATE_DATA: return ("data");
81 case ARCHIVE_STATE_EOF: return ("eof");
82 case ARCHIVE_STATE_CLOSED: return ("closed");
83 case ARCHIVE_STATE_FATAL: return ("fatal");
84 default: return ("??");
85 }
86 }
87
88 static const char *
archive_handle_type_name(unsigned m)89 archive_handle_type_name(unsigned m)
90 {
91 switch (m) {
92 case ARCHIVE_WRITE_MAGIC: return ("archive_write");
93 case ARCHIVE_READ_MAGIC: return ("archive_read");
94 case ARCHIVE_WRITE_DISK_MAGIC: return ("archive_write_disk");
95 case ARCHIVE_READ_DISK_MAGIC: return ("archive_read_disk");
96 case ARCHIVE_MATCH_MAGIC: return ("archive_match");
97 default: return NULL;
98 }
99 }
100
101 static void
write_all_states(char * buff,unsigned int states)102 write_all_states(char *buff, unsigned int states)
103 {
104 unsigned int lowbit;
105
106 *buff = '\0';
107
108 /* A trick for computing the lowest set bit. */
109 while ((lowbit = states & (1 + ~states)) != 0) {
110 states &= ~lowbit; /* Clear the low bit. */
111 strcat(buff, state_name(lowbit));
112 if (states != 0)
113 strcat(buff, "/");
114 }
115 }
116
117 /*
118 * Check magic value and current state.
119 * Magic value mismatches are fatal and result in calls to abort().
120 * State mismatches return ARCHIVE_FATAL.
121 * Otherwise, returns ARCHIVE_OK.
122 *
123 * This is designed to catch serious programming errors that violate
124 * the libarchive API.
125 */
126 int
__archive_check_magic(struct archive * a,unsigned int magic,unsigned int state,const char * function)127 __archive_check_magic(struct archive *a, unsigned int magic,
128 unsigned int state, const char *function)
129 {
130 char states1[64];
131 char states2[64];
132 const char *handle_type;
133
134 /*
135 * If this isn't some form of archive handle,
136 * then the library user has screwed up so bad that
137 * we don't even have a reliable way to report an error.
138 */
139 handle_type = archive_handle_type_name(a->magic);
140
141 if (!handle_type) {
142 errmsg("PROGRAMMER ERROR: Function ");
143 errmsg(function);
144 errmsg(" invoked with invalid archive handle.\n");
145 diediedie();
146 }
147
148 if (a->magic != magic) {
149 archive_set_error(a, -1,
150 "PROGRAMMER ERROR: Function '%s' invoked"
151 " on '%s' archive object, which is not supported.",
152 function,
153 handle_type);
154 a->state = ARCHIVE_STATE_FATAL;
155 return (ARCHIVE_FATAL);
156 }
157
158 if ((a->state & state) == 0) {
159 /* If we're already FATAL, don't overwrite the error. */
160 if (a->state != ARCHIVE_STATE_FATAL) {
161 write_all_states(states1, a->state);
162 write_all_states(states2, state);
163 archive_set_error(a, -1,
164 "INTERNAL ERROR: Function '%s' invoked with"
165 " archive structure in state '%s',"
166 " should be in state '%s'",
167 function,
168 states1,
169 states2);
170 }
171 a->state = ARCHIVE_STATE_FATAL;
172 return (ARCHIVE_FATAL);
173 }
174 return (ARCHIVE_OK);
175 }
176