archive_check_magic.c (228763) | archive_check_magic.c (232153) |
---|---|
1/*- | 1/*- |
2 * Copyright (c) 2003-2007 Tim Kientzle | 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 --- 8 unchanged lines hidden (view full) --- 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" | 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 --- 8 unchanged lines hidden (view full) --- 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: head/contrib/libarchive/libarchive/archive_check_magic.c 228763 2011-12-21 11:13:29Z mm $"); | 27__FBSDID("$FreeBSD: head/contrib/libarchive/libarchive/archive_check_magic.c 232153 2012-02-25 10:58:02Z mm $"); |
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> --- 45 unchanged lines hidden (view full) --- 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 | 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> --- 45 unchanged lines hidden (view full) --- 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 |
89static const char * 90archive_handle_type_name(unsigned m) 91{ 92 switch (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 default: return NULL; 98 } 99} |
|
89 | 100 |
90static void 91write_all_states(unsigned int states) | 101 102static char * 103write_all_states(char *buff, unsigned int states) |
92{ 93 unsigned int lowbit; 94 | 104{ 105 unsigned int lowbit; 106 |
107 buff[0] = '\0'; 108 |
|
95 /* A trick for computing the lowest set bit. */ 96 while ((lowbit = states & (1 + ~states)) != 0) { 97 states &= ~lowbit; /* Clear the low bit. */ | 109 /* A trick for computing the lowest set bit. */ 110 while ((lowbit = states & (1 + ~states)) != 0) { 111 states &= ~lowbit; /* Clear the low bit. */ |
98 errmsg(state_name(lowbit)); | 112 strcat(buff, state_name(lowbit)); |
99 if (states != 0) | 113 if (states != 0) |
100 errmsg("/"); | 114 strcat(buff, "/"); |
101 } | 115 } |
116 return buff; |
|
102} 103 104/* | 117} 118 119/* |
105 * Check magic value and current state; bail if it isn't valid. | 120 * Check magic value and current state. 121 * Magic value mismatches are fatal and result in calls to abort(). 122 * State mismatches return ARCHIVE_FATAL. 123 * Otherwise, returns ARCHIVE_OK. |
106 * 107 * This is designed to catch serious programming errors that violate 108 * the libarchive API. 109 */ | 124 * 125 * This is designed to catch serious programming errors that violate 126 * the libarchive API. 127 */ |
110void | 128int |
111__archive_check_magic(struct archive *a, unsigned int magic, 112 unsigned int state, const char *function) 113{ | 129__archive_check_magic(struct archive *a, unsigned int magic, 130 unsigned int state, const char *function) 131{ |
114 if (a->magic != magic) { 115 errmsg("INTERNAL ERROR: Function "); | 132 char states1[64]; 133 char states2[64]; 134 const char *handle_type; 135 136 /* 137 * If this isn't some form of archive handle, 138 * then the library user has screwed up so bad that 139 * we don't even have a reliable way to report an error. 140 */ 141 handle_type = archive_handle_type_name(a->magic); 142 143 if (!handle_type) { 144 errmsg("PROGRAMMER ERROR: Function "); |
116 errmsg(function); | 145 errmsg(function); |
117 errmsg(" invoked with invalid struct archive structure.\n"); | 146 errmsg(" invoked with invalid archive handle.\n"); |
118 diediedie(); 119 } 120 | 147 diediedie(); 148 } 149 |
121 if (state == ARCHIVE_STATE_ANY) 122 return; | 150 if (a->magic != magic) { 151 archive_set_error(a, -1, 152 "PROGRAMMER ERROR: Function '%s' invoked" 153 " on '%s' archive object, which is not supported.", 154 function, 155 handle_type); 156 a->state = ARCHIVE_STATE_FATAL; 157 return (ARCHIVE_FATAL); 158 } |
123 124 if ((a->state & state) == 0) { | 159 160 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(); | 161 /* If we're already FATAL, don't overwrite the error. */ 162 if (a->state != ARCHIVE_STATE_FATAL) 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 write_all_states(states1, a->state), 169 write_all_states(states2, state)); 170 a->state = ARCHIVE_STATE_FATAL; 171 return (ARCHIVE_FATAL); |
133 } | 172 } |
173 return ARCHIVE_OK; |
|
134} | 174} |