1 #ifndef _HAD_ZIPINT_H 2 #define _HAD_ZIPINT_H 3 4 /* 5 zipint.h -- internal declarations. 6 Copyright (C) 1999-2011 Dieter Baron and Thomas Klausner 7 8 This file is part of libzip, a library to manipulate ZIP archives. 9 The authors can be contacted at <libzip@nih.at> 10 11 Redistribution and use in source and binary forms, with or without 12 modification, are permitted provided that the following conditions 13 are met: 14 1. Redistributions of source code must retain the above copyright 15 notice, this list of conditions and the following disclaimer. 16 2. Redistributions in binary form must reproduce the above copyright 17 notice, this list of conditions and the following disclaimer in 18 the documentation and/or other materials provided with the 19 distribution. 20 3. The names of the authors may not be used to endorse or promote 21 products derived from this software without specific prior 22 written permission. 23 24 THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS 25 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY 28 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 30 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 31 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 32 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 33 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 34 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 */ 36 37 #include <zlib.h> 38 39 #include "zip.h" 40 41 #ifndef HAVE_FSEEKO 42 #define fseeko(s, o, w) (fseek((s), (long int)(o), (w))) 43 #endif 44 45 #ifndef HAVE_FTELLO 46 #define ftello(s) ((long)ftell((s))) 47 #endif 48 49 #ifndef PHP_WIN32 50 #ifndef HAVE_MKSTEMP 51 int _zip_mkstemp(char *); 52 #define mkstemp _zip_mkstemp 53 #endif 54 #endif 55 56 #ifdef PHP_WIN32 57 #include <windows.h> 58 #include <wchar.h> 59 #define _zip_rename(s, t) \ 60 (!MoveFileExA((s), (t), \ 61 MOVEFILE_COPY_ALLOWED|MOVEFILE_REPLACE_EXISTING)) 62 63 /* for dup(), close(), etc. */ 64 #include <io.h> 65 66 #if !defined(HAVE_OPEN) 67 #if defined(HAVE__OPEN) 68 #define open(a, b, c) _open((a), (b)) 69 #endif 70 #endif 71 72 #else 73 #define _zip_rename rename 74 #endif 75 76 #ifndef strcasecmp 77 # define strcmpi strcasecmp 78 #endif 79 80 81 82 83 #define CENTRAL_MAGIC "PK\1\2" 84 #define LOCAL_MAGIC "PK\3\4" 85 #define EOCD_MAGIC "PK\5\6" 86 #define DATADES_MAGIC "PK\7\8" 87 #define TORRENT_SIG "TORRENTZIPPED-" 88 #define TORRENT_SIG_LEN 14 89 #define TORRENT_CRC_LEN 8 90 #define TORRENT_MEM_LEVEL 8 91 #define CDENTRYSIZE 46u 92 #define LENTRYSIZE 30 93 #undef MAXCOMLEN /* defined as 19 on BSD for max command name */ 94 #define MAXCOMLEN 65536 95 #define MAXEXTLEN 65536 96 #define EOCDLEN 22 97 #define CDBUFSIZE (MAXCOMLEN+EOCDLEN) 98 #define BUFSIZE 8192 99 100 101 102 /* This section contains API that won't materialize like this. It's 103 placed in the internal section, pending cleanup. */ 104 105 typedef struct zip_source *(*zip_compression_implementation)(struct zip *, 106 struct zip_source *, 107 zip_uint16_t, int); 108 typedef struct zip_source *(*zip_encryption_implementation)(struct zip *, 109 struct zip_source *, 110 zip_uint16_t, int, 111 const char *); 112 113 ZIP_EXTERN(zip_compression_implementation) zip_get_compression_implementation( 114 zip_uint16_t); 115 ZIP_EXTERN(zip_encryption_implementation) zip_get_encryption_implementation( 116 zip_uint16_t); 117 118 119 120 121 /* This section contains API that is of limited use until support for 122 user-supplied compression/encryption implementation is finished. 123 Thus we will keep it private for now. */ 124 125 typedef zip_int64_t (*zip_source_layered_callback)(struct zip_source *, void *, 126 void *, zip_uint64_t, 127 enum zip_source_cmd); 128 129 ZIP_EXTERN(void) zip_source_close(struct zip_source *); 130 ZIP_EXTERN(struct zip_source *)zip_source_crc(struct zip *, struct zip_source *, 131 int); 132 ZIP_EXTERN(struct zip_source *)zip_source_deflate(struct zip *, 133 struct zip_source *, 134 zip_uint16_t, int); 135 ZIP_EXTERN(void) zip_source_error(struct zip_source *, int *, int *); 136 ZIP_EXTERN(struct zip_source *)zip_source_layered(struct zip *, 137 struct zip_source *, 138 zip_source_layered_callback, 139 void *); 140 ZIP_EXTERN(int) zip_source_open(struct zip_source *); 141 ZIP_EXTERN(struct zip_source *)zip_source_pkware(struct zip *, 142 struct zip_source *, 143 zip_uint16_t, int, 144 const char *); 145 ZIP_EXTERN(zip_int64_t) zip_source_read(struct zip_source *, void *, 146 zip_uint64_t); 147 ZIP_EXTERN(int) zip_source_stat(struct zip_source *, struct zip_stat *); 148 149 150 /* This function will probably remain private. It is not needed to 151 implement compression/encryption routines. (We should probably 152 rename it to _zip_source_pop.) */ 153 154 ZIP_EXTERN(struct zip_source *)zip_source_pop(struct zip_source *); 155 156 157 158 /* state of change of a file in zip archive */ 159 160 enum zip_state { ZIP_ST_UNCHANGED, ZIP_ST_DELETED, ZIP_ST_REPLACED, 161 ZIP_ST_ADDED, ZIP_ST_RENAMED }; 162 163 /* error source for layered sources */ 164 165 enum zip_les { ZIP_LES_NONE, ZIP_LES_UPPER, ZIP_LES_LOWER, ZIP_LES_INVAL }; 166 167 /* directory entry: general purpose bit flags */ 168 169 #define ZIP_GPBF_ENCRYPTED 0x0001 /* is encrypted */ 170 #define ZIP_GPBF_DATA_DESCRIPTOR 0x0008 /* crc/size after file data */ 171 #define ZIP_GPBF_STRONG_ENCRYPTION 0x0040 /* uses strong encryption */ 172 173 /* error information */ 174 175 struct zip_error { 176 int zip_err; /* libzip error code (ZIP_ER_*) */ 177 int sys_err; /* copy of errno (E*) or zlib error code */ 178 char *str; /* string representation or NULL */ 179 }; 180 181 /* zip archive, part of API */ 182 183 struct zip { 184 char *zn; /* file name */ 185 FILE *zp; /* file */ 186 struct zip_error error; /* error information */ 187 188 unsigned int flags; /* archive global flags */ 189 unsigned int ch_flags; /* changed archive global flags */ 190 191 char *default_password; /* password used when no other supplied */ 192 193 struct zip_cdir *cdir; /* central directory */ 194 char *ch_comment; /* changed archive comment */ 195 int ch_comment_len; /* length of changed zip archive 196 * comment, -1 if unchanged */ 197 zip_uint64_t nentry; /* number of entries */ 198 zip_uint64_t nentry_alloc; /* number of entries allocated */ 199 struct zip_entry *entry; /* entries */ 200 int nfile; /* number of opened files within archive */ 201 int nfile_alloc; /* number of files allocated */ 202 struct zip_file **file; /* opened files within archive */ 203 }; 204 205 /* file in zip archive, part of API */ 206 207 struct zip_file { 208 struct zip *za; /* zip archive containing this file */ 209 struct zip_error error; /* error information */ 210 int eof; 211 struct zip_source *src; /* data source */ 212 }; 213 214 /* zip archive directory entry (central or local) */ 215 216 struct zip_dirent { 217 unsigned short version_madeby; /* (c) version of creator */ 218 unsigned short version_needed; /* (cl) version needed to extract */ 219 unsigned short bitflags; /* (cl) general purpose bit flag */ 220 unsigned short comp_method; /* (cl) compression method used */ 221 time_t last_mod; /* (cl) time of last modification */ 222 unsigned int crc; /* (cl) CRC-32 of uncompressed data */ 223 unsigned int comp_size; /* (cl) size of commpressed data */ 224 unsigned int uncomp_size; /* (cl) size of uncommpressed data */ 225 char *filename; /* (cl) file name (NUL-terminated) */ 226 unsigned short filename_len; /* (cl) length of filename (w/o NUL) */ 227 char *extrafield; /* (cl) extra field */ 228 unsigned short extrafield_len; /* (cl) length of extra field */ 229 char *comment; /* (c) file comment */ 230 unsigned short comment_len; /* (c) length of file comment */ 231 unsigned short disk_number; /* (c) disk number start */ 232 unsigned short int_attrib; /* (c) internal file attributes */ 233 unsigned int ext_attrib; /* (c) external file attributes */ 234 unsigned int offset; /* (c) offset of local header */ 235 }; 236 237 /* zip archive central directory */ 238 239 struct zip_cdir { 240 struct zip_dirent *entry; /* directory entries */ 241 int nentry; /* number of entries */ 242 243 unsigned int size; /* size of central direcotry */ 244 unsigned int offset; /* offset of central directory in file */ 245 char *comment; /* zip archive comment */ 246 unsigned short comment_len; /* length of zip archive comment */ 247 }; 248 249 250 251 struct zip_source { 252 struct zip_source *src; 253 union { 254 zip_source_callback f; 255 zip_source_layered_callback l; 256 } cb; 257 void *ud; 258 enum zip_les error_source; 259 int is_open; 260 }; 261 262 /* entry in zip archive directory */ 263 264 struct zip_entry { 265 enum zip_state state; 266 struct zip_source *source; 267 char *ch_filename; 268 char *ch_extra; 269 int ch_extra_len; 270 char *ch_comment; 271 int ch_comment_len; 272 }; 273 274 275 276 extern const char * const _zip_err_str[]; 277 extern const int _zip_nerr_str; 278 extern const int _zip_err_type[]; 279 280 281 282 #define ZIP_ENTRY_DATA_CHANGED(x) \ 283 ((x)->state == ZIP_ST_REPLACED \ 284 || (x)->state == ZIP_ST_ADDED) 285 286 #define ZIP_IS_RDONLY(za) ((za)->ch_flags & ZIP_AFL_RDONLY) 287 288 289 290 int _zip_cdir_compute_crc(struct zip *, uLong *); 291 void _zip_cdir_free(struct zip_cdir *); 292 int _zip_cdir_grow(struct zip_cdir *, int, struct zip_error *); 293 struct zip_cdir *_zip_cdir_new(int, struct zip_error *); 294 int _zip_cdir_write(struct zip_cdir *, FILE *, struct zip_error *); 295 296 void _zip_dirent_finalize(struct zip_dirent *); 297 void _zip_dirent_init(struct zip_dirent *); 298 int _zip_dirent_read(struct zip_dirent *, FILE *, unsigned char **, 299 zip_uint32_t *, int, struct zip_error *); 300 void _zip_dirent_torrent_normalize(struct zip_dirent *); 301 int _zip_dirent_write(struct zip_dirent *, FILE *, int, struct zip_error *); 302 303 void _zip_entry_free(struct zip_entry *); 304 void _zip_entry_init(struct zip *, int); 305 struct zip_entry *_zip_entry_new(struct zip *); 306 307 void _zip_error_clear(struct zip_error *); 308 void _zip_error_copy(struct zip_error *, struct zip_error *); 309 void _zip_error_fini(struct zip_error *); 310 void _zip_error_get(struct zip_error *, int *, int *); 311 void _zip_error_init(struct zip_error *); 312 void _zip_error_set(struct zip_error *, int, int); 313 void _zip_error_set_from_source(struct zip_error *, struct zip_source *); 314 const char *_zip_error_strerror(struct zip_error *); 315 316 int _zip_file_fillbuf(void *, size_t, struct zip_file *); 317 unsigned int _zip_file_get_offset(struct zip *, int); 318 319 int _zip_filerange_crc(FILE *, off_t, off_t, uLong *, struct zip_error *); 320 321 struct zip *_zip_open(const char *, FILE *, int, int, int *); 322 323 struct zip_source *_zip_source_file_or_p(struct zip *, const char *, FILE *, 324 zip_uint64_t, zip_int64_t, int, 325 const struct zip_stat *); 326 struct zip_source *_zip_source_new(struct zip *); 327 328 int _zip_changed(struct zip *, int *); 329 void _zip_free(struct zip *); 330 const char *_zip_get_name(struct zip *, zip_uint64_t, int, struct zip_error *); 331 int _zip_local_header_read(struct zip *, int); 332 void *_zip_memdup(const void *, size_t, struct zip_error *); 333 int _zip_name_locate(struct zip *, const char *, int, struct zip_error *); 334 struct zip *_zip_new(struct zip_error *); 335 unsigned short _zip_read2(unsigned char **); 336 unsigned int _zip_read4(unsigned char **); 337 zip_int64_t _zip_replace(struct zip *, zip_uint64_t, const char *, 338 struct zip_source *); 339 int _zip_set_name(struct zip *, zip_uint64_t, const char *); 340 void _zip_u2d_time(time_t, unsigned short *, unsigned short *); 341 int _zip_unchange(struct zip *, zip_uint64_t, int); 342 void _zip_unchange_data(struct zip_entry *); 343 344 #endif /* zipint.h */ 345