1 /*-
2 * Copyright (c) 2003-2007 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 __FBSDID("$FreeBSD: src/lib/libarchive/archive_check_magic.c,v 1.9 2008/12/06 05:52:01 kientzle Exp $");
28
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
31 #endif
32
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
43 #if defined(_WIN32) && !defined(__CYGWIN__)
44 #include <windows.h>
45 #include <winbase.h>
46 #endif
47
48 #include "archive_private.h"
49
50 static void
errmsg(const char * m)51 errmsg(const char *m)
52 {
53 size_t s = strlen(m);
54 ssize_t written;
55
56 while (s > 0) {
57 written = write(2, m, strlen(m));
58 if (written <= 0)
59 return;
60 m += written;
61 s -= written;
62 }
63 }
64
65 static void
diediedie(void)66 diediedie(void)
67 {
68 #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
69 /* Cause a breakpoint exception */
70 DebugBreak();
71 #endif
72 abort(); /* Terminate the program abnormally. */
73 }
74
75 static const char *
state_name(unsigned s)76 state_name(unsigned s)
77 {
78 switch (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 ("??");
86 }
87 }
88
89
90 static void
write_all_states(unsigned int states)91 write_all_states(unsigned int states)
92 {
93 unsigned int lowbit;
94
95 /* A trick for computing the lowest set bit. */
96 while ((lowbit = states & (-states)) != 0) {
97 states &= ~lowbit; /* Clear the low bit. */
98 errmsg(state_name(lowbit));
99 if (states != 0)
100 errmsg("/");
101 }
102 }
103
104 /*
105 * Check magic value and current state; bail if it isn't valid.
106 *
107 * This is designed to catch serious programming errors that violate
108 * the libarchive API.
109 */
110 void
__archive_check_magic(struct archive * a,unsigned int magic,unsigned int state,const char * function)111 __archive_check_magic(struct archive *a, unsigned int magic,
112 unsigned int state, const char *function)
113 {
114 if (a->magic != magic) {
115 errmsg("INTERNAL ERROR: Function ");
116 errmsg(function);
117 errmsg(" invoked with invalid struct archive structure.\n");
118 diediedie();
119 }
120
121 if (state == ARCHIVE_STATE_ANY)
122 return;
123
124 if ((a->state & state) == 0) {
125 errmsg("INTERNAL ERROR: Function '");
126 errmsg(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);
131 errmsg("'\n");
132 diediedie();
133 }
134 }
135