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
51 errmsg(const char *m)
52 {
53 	write(2, m, strlen(m));
54 }
55 
56 static void
57 diediedie(void)
58 {
59 #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
60 	/* Cause a breakpoint exception  */
61 	DebugBreak();
62 #endif
63 	*(char *)0 = 1;	/* Deliberately segfault and force a coredump. */
64 	_exit(1);	/* If that didn't work, just exit with an error. */
65 }
66 
67 static const char *
68 state_name(unsigned s)
69 {
70 	switch (s) {
71 	case ARCHIVE_STATE_NEW:		return ("new");
72 	case ARCHIVE_STATE_HEADER:	return ("header");
73 	case ARCHIVE_STATE_DATA:	return ("data");
74 	case ARCHIVE_STATE_EOF:		return ("eof");
75 	case ARCHIVE_STATE_CLOSED:	return ("closed");
76 	case ARCHIVE_STATE_FATAL:	return ("fatal");
77 	default:			return ("??");
78 	}
79 }
80 
81 
82 static void
83 write_all_states(unsigned int states)
84 {
85 	unsigned int lowbit;
86 
87 	/* A trick for computing the lowest set bit. */
88 	while ((lowbit = states & (-states)) != 0) {
89 		states &= ~lowbit;		/* Clear the low bit. */
90 		errmsg(state_name(lowbit));
91 		if (states != 0)
92 			errmsg("/");
93 	}
94 }
95 
96 /*
97  * Check magic value and current state; bail if it isn't valid.
98  *
99  * This is designed to catch serious programming errors that violate
100  * the libarchive API.
101  */
102 void
103 __archive_check_magic(struct archive *a, unsigned int magic,
104     unsigned int state, const char *function)
105 {
106 	if (a->magic != magic) {
107 		errmsg("INTERNAL ERROR: Function ");
108 		errmsg(function);
109 		errmsg(" invoked with invalid struct archive structure.\n");
110 		diediedie();
111 	}
112 
113 	if (state == ARCHIVE_STATE_ANY)
114 		return;
115 
116 	if ((a->state & state) == 0) {
117 		errmsg("INTERNAL ERROR: Function '");
118 		errmsg(function);
119 		errmsg("' invoked with archive structure in state '");
120 		write_all_states(a->state);
121 		errmsg("', should be in state '");
122 		write_all_states(state);
123 		errmsg("'\n");
124 		diediedie();
125 	}
126 }
127