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 #ifndef ARCHIVE_PRIVATE_H_INCLUDED 27 #define ARCHIVE_PRIVATE_H_INCLUDED 28 29 #ifndef __LIBARCHIVE_BUILD 30 #ifndef __LIBARCHIVE_TEST 31 #error This header is only to be used internally to libarchive. 32 #endif 33 #endif 34 35 #if HAVE_ICONV_H 36 #include <iconv.h> 37 #endif 38 39 #include "archive.h" 40 #include "archive_string.h" 41 42 #if defined(__GNUC__) && (__GNUC__ > 2 || \ 43 (__GNUC__ == 2 && __GNUC_MINOR__ >= 5)) 44 #define __LA_NORETURN __attribute__((__noreturn__)) 45 #elif defined(_MSC_VER) 46 #define __LA_NORETURN __declspec(noreturn) 47 #else 48 #define __LA_NORETURN 49 #endif 50 51 #if defined(__GNUC__) && (__GNUC__ > 2 || \ 52 (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)) 53 #define __LA_UNUSED __attribute__((__unused__)) 54 #else 55 #define __LA_UNUSED 56 #endif 57 58 #define ARCHIVE_WRITE_MAGIC (0xb0c5c0deU) 59 #define ARCHIVE_READ_MAGIC (0xdeb0c5U) 60 #define ARCHIVE_WRITE_DISK_MAGIC (0xc001b0c5U) 61 #define ARCHIVE_READ_DISK_MAGIC (0xbadb0c5U) 62 #define ARCHIVE_MATCH_MAGIC (0xcad11c9U) 63 64 /* 65 * Having the state be a bitmask makes it easy to check 66 * for combinations of allowed states. These should 67 * generally only be used in public API entry points, 68 * primarily in archive_read.c, archive_write.c, etc. 69 * Internal callbacks can rely on the core machinery 70 * to only call them when appropriate. 71 * 72 * Generally checked via `__archive_check_magic()`. 73 */ 74 75 /* Newly created archive object, not yet opened */ 76 #define ARCHIVE_STATE_NEW 1U 77 /* Archive is ready to read a header. */ 78 #define ARCHIVE_STATE_HEADER 2U 79 /* A header has been read: client can ask for data 80 * or they can ask for the next header and 81 * we'll automatically skip the remaining data. */ 82 #define ARCHIVE_STATE_DATA 4U 83 /* Similar to STATE_DATA but after a FAILED header: 84 * the client may not read data, but they may ask 85 * for the next header and we'll recover. */ 86 #define ARCHIVE_STATE_DATA_RECOVERY 8U 87 /* End-of-archive has been reached. Client can only 88 * close or free the archive. */ 89 #define ARCHIVE_STATE_EOF 0x10U 90 /* Archive is closed; client is only allowed to free the archive. */ 91 #define ARCHIVE_STATE_CLOSED 0x20U 92 /* Archive is in a FATAL error state: a close request 93 * is permitted but ignored. */ 94 #define ARCHIVE_STATE_FATAL 0x8000U 95 /* Any valid (non-fatal) state. */ 96 #define ARCHIVE_STATE_ANY (0xFFFFU & ~ARCHIVE_STATE_FATAL) 97 98 struct archive_vtable { 99 int (*archive_close)(struct archive *); 100 int (*archive_free)(struct archive *); 101 int (*archive_write_header)(struct archive *, 102 struct archive_entry *); 103 int (*archive_write_finish_entry)(struct archive *); 104 ssize_t (*archive_write_data)(struct archive *, 105 const void *, size_t); 106 ssize_t (*archive_write_data_block)(struct archive *, 107 const void *, size_t, int64_t); 108 109 int (*archive_read_next_header)(struct archive *, 110 struct archive_entry **); 111 int (*archive_read_next_header2)(struct archive *, 112 struct archive_entry *); 113 int (*archive_read_data_block)(struct archive *, 114 const void **, size_t *, int64_t *); 115 116 int (*archive_filter_count)(struct archive *); 117 int64_t (*archive_filter_bytes)(struct archive *, int); 118 int (*archive_filter_code)(struct archive *, int); 119 const char * (*archive_filter_name)(struct archive *, int); 120 }; 121 122 struct archive_string_conv; 123 124 struct archive { 125 /* 126 * The magic/state values are used to sanity-check the 127 * client's usage. If an API function is called at a 128 * ridiculous time, or the client passes us an invalid 129 * pointer, these values allow me to catch that. 130 */ 131 unsigned int magic; 132 unsigned int state; 133 134 /* 135 * Some public API functions depend on the "real" type of the 136 * archive object. 137 */ 138 const struct archive_vtable *vtable; 139 140 int archive_format; 141 const char *archive_format_name; 142 143 /* Number of file entries processed. */ 144 int file_count; 145 146 int archive_error_number; 147 const char *error; 148 struct archive_string error_string; 149 150 char *current_code; 151 unsigned current_codepage; /* Current ACP(ANSI CodePage). */ 152 unsigned current_oemcp; /* Current OEMCP(OEM CodePage). */ 153 struct archive_string_conv *sconv; 154 155 /* 156 * Used by archive_read_data() to track blocks and copy 157 * data to client buffers, filling gaps with zero bytes. 158 */ 159 const char *read_data_block; 160 int64_t read_data_offset; 161 int64_t read_data_output_offset; 162 size_t read_data_remaining; 163 164 /* 165 * Used by formats/filters to determine the amount of data 166 * requested from a call to archive_read_data(). This is only 167 * useful when the format/filter has seek support. 168 */ 169 char read_data_is_posix_read; 170 size_t read_data_requested; 171 }; 172 173 /* Check magic value and state; return(ARCHIVE_FATAL) if it isn't valid. */ 174 int __archive_check_magic(struct archive *, unsigned int magic, 175 unsigned int state, const char *func); 176 #define archive_check_magic(a, expected_magic, allowed_states, function_name) \ 177 do { \ 178 int magic_test = __archive_check_magic((a), (expected_magic), \ 179 (allowed_states), (function_name)); \ 180 if (magic_test == ARCHIVE_FATAL) \ 181 return ARCHIVE_FATAL; \ 182 } while (0) 183 184 __LA_NORETURN void __archive_errx(int retvalue, const char *msg); 185 186 void __archive_ensure_cloexec_flag(int fd); 187 int __archive_get_tempdir(struct archive_string *); 188 int __archive_mktemp(const char *tmpdir); 189 #if defined(_WIN32) && !defined(__CYGWIN__) 190 int __archive_mkstemp(wchar_t *templates); 191 #else 192 int __archive_mkstemp(char *templates); 193 #endif 194 195 int __archive_clean(struct archive *); 196 197 void __archive_reset_read_data(struct archive *); 198 199 #define err_combine(a,b) ((a) < (b) ? (a) : (b)) 200 201 #if defined(__BORLANDC__) || (defined(_MSC_VER) && _MSC_VER <= 1300) 202 # define ARCHIVE_LITERAL_LL(x) x##i64 203 # define ARCHIVE_LITERAL_ULL(x) x##ui64 204 #else 205 # define ARCHIVE_LITERAL_LL(x) x##ll 206 # define ARCHIVE_LITERAL_ULL(x) x##ull 207 #endif 208 209 #endif 210