unzip.h (8373B)
1 /* 2 This file is part of libextractor. 3 Copyright (C) 2008, 2012 Christian Grothoff (and other contributing authors) 4 5 libextractor is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published 7 by the Free Software Foundation; either version 3, or (at your 8 option) any later version. 9 10 libextractor is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with libextractor; see the file COPYING. If not, write to the 17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 18 Boston, MA 02110-1301, USA. 19 */ 20 /** 21 * @file common/unzip.h 22 * @brief API to access ZIP archives 23 * @author Christian Grothoff 24 * 25 * This code is based in part on 26 * unzip 1.00 Copyright 1998-2003 Gilles Vollant 27 * http://www.winimage.com/zLibDll" 28 */ 29 #ifndef LE_COMMON_UNZIP_H 30 #define LE_COMMON_UNZIP_H 31 32 #include <zlib.h> 33 34 /** 35 * Operation was successful. 36 */ 37 #define EXTRACTOR_UNZIP_OK (0) 38 39 /** 40 * Cannot move to next file, we are at the end 41 */ 42 #define EXTRACTOR_UNZIP_END_OF_LIST_OF_FILE (-100) 43 44 /** 45 * IO error, see errno. 46 */ 47 #define EXTRACTOR_UNZIP_ERRNO (Z_ERRNO) 48 49 /** 50 * Reached end of the file (NOTE: same as OK!) 51 */ 52 #define EXTRACTOR_UNZIP_EOF (0) 53 54 /** 55 * Invalid arguments to call. 56 */ 57 #define EXTRACTOR_UNZIP_PARAMERROR (-102) 58 59 /** 60 * Not a zip file (or malformed) 61 */ 62 #define EXTRACTOR_UNZIP_BADZIPFILE (-103) 63 64 /** 65 * Internal error. 66 */ 67 #define EXTRACTOR_UNZIP_INTERNALERROR (-104) 68 69 /** 70 * Checksum failure. 71 */ 72 #define EXTRACTOR_UNZIP_CRCERROR (-105) 73 74 /** 75 * Handle for a ZIP archive. 76 */ 77 struct EXTRACTOR_UnzipFile; 78 79 80 /** 81 * date/time information 82 */ 83 struct EXTRACTOR_UnzipDateTimeInfo 84 { 85 /** 86 * seconds after the minute - [0,59] 87 */ 88 uInt tm_sec; 89 90 /** 91 * minutes after the hour - [0,59] 92 */ 93 uInt tm_min; 94 95 /** 96 * hours since midnight - [0,23] 97 */ 98 uInt tm_hour; 99 100 /** 101 * day of the month - [1,31] 102 */ 103 uInt tm_mday; 104 105 /** 106 * months since January - [0,11] 107 */ 108 uInt tm_mon; 109 110 /** 111 * years - [1980..2044] 112 */ 113 uInt tm_year; 114 }; 115 116 117 /** 118 * Information about a file in the zipfile 119 */ 120 struct EXTRACTOR_UnzipFileInfo 121 { 122 /** 123 * version made by 2 bytes 124 */ 125 uLong version; 126 127 /** 128 * version needed to extract 2 bytes 129 */ 130 uLong version_needed; 131 132 /** 133 * general purpose bit flag 2 bytes 134 */ 135 uLong flag; 136 137 /** 138 * compression method 2 bytes 139 */ 140 uLong compression_method; 141 142 /** 143 * last mod file date in Dos fmt 4 bytes 144 */ 145 uLong dosDate; 146 147 /** 148 * crc-32 4 bytes 149 */ 150 uLong crc; 151 152 /** 153 * compressed size 4 bytes 154 */ 155 uLong compressed_size; 156 157 /** 158 * uncompressed size 4 bytes 159 */ 160 uLong uncompressed_size; 161 162 /** 163 * filename length 2 bytes 164 */ 165 uLong size_filename; 166 167 /** 168 * extra field length 2 bytes 169 */ 170 uLong size_file_extra; 171 172 /** 173 * file comment length 2 bytes 174 */ 175 uLong size_file_comment; 176 177 /** 178 * disk number start 2 bytes 179 */ 180 uLong disk_num_start; 181 182 /** 183 * internal file attributes 2 bytes 184 */ 185 uLong internal_fa; 186 187 /** 188 * external file attributes 4 bytes 189 */ 190 uLong external_fa; 191 192 /** 193 * Time and date of last modification. 194 */ 195 struct EXTRACTOR_UnzipDateTimeInfo tmu_date; 196 }; 197 198 199 /** 200 * Open a zip file for processing using the data access 201 * functions from the extract context. 202 * 203 * @param ec extract context to use 204 * @return handle to zip data, NULL on error 205 */ 206 struct EXTRACTOR_UnzipFile * 207 EXTRACTOR_common_unzip_open (struct EXTRACTOR_ExtractContext *ec); 208 209 210 /** 211 * Obtain the global comment from a ZIP file. 212 * 213 * @param file unzip file to inspect 214 * @param comment where to copy the comment 215 * @param comment_len maximum number of bytes available in comment 216 * @return EXTRACTOR_UNZIP_OK on success 217 */ 218 int 219 EXTRACTOR_common_unzip_get_global_comment (struct EXTRACTOR_UnzipFile *file, 220 char *comment, 221 size_t comment_len); 222 223 224 /** 225 * Close a ZipFile. 226 * 227 * @param file zip file to close 228 * @return EXTRACTOR_UNZIP_OK if there is no problem. 229 */ 230 int 231 EXTRACTOR_common_unzip_close (struct EXTRACTOR_UnzipFile *file); 232 233 234 /** 235 * Set the current file of the zipfile to the first file. 236 * 237 * @param file zipfile to manipulate 238 * @return UNZ_OK if there is no problem 239 */ 240 int 241 EXTRACTOR_common_unzip_go_to_first_file (struct EXTRACTOR_UnzipFile *file); 242 243 244 /** 245 * Set the current file of the zipfile to the next file. 246 * 247 * @param file zipfile to manipulate 248 * @return EXTRACTOR_UNZIP_OK if there is no problem, 249 * EXTRACTOR_UNZIP_END_OF_LIST_OF_FILE if the actual file was the latest. 250 */ 251 int 252 EXTRACTOR_common_unzip_go_to_next_file (struct EXTRACTOR_UnzipFile *file); 253 254 255 /** 256 * Try locate the file szFileName in the zipfile. 257 * 258 * @param file zipfile to manipulate 259 * @param szFileName name to find 260 * @param iCaseSensitivity, use 1 for case sensitivity (like strcmp); 261 * 2 for no case sensitivity (like strcmpi or strcasecmp); or 262 * 0 for defaut of your operating system (like 1 on Unix, 2 on Windows) 263 * @return EXTRACTOR_UNZIP_OK if the file is found. It becomes the current file. 264 * EXTRACTOR_UNZIP_END_OF_LIST_OF_FILE if the file is not found 265 */ 266 int 267 EXTRACTOR_common_unzip_go_find_local_file (struct EXTRACTOR_UnzipFile *file, 268 const char *szFileName, 269 int iCaseSensitivity); 270 271 272 /** 273 * Write info about the ZipFile in the *pglobal_info structure. 274 * No preparation of the structure is needed. 275 * 276 * @param file zipfile to manipulate 277 * @param pfile_info file information to initialize 278 * @param szFileName where to write the name of the current file 279 * @param fileNameBufferSize number of bytes available in szFileName 280 * @param extraField where to write extra data 281 * @param extraFieldBufferSize number of bytes available in extraField 282 * @param szComment where to write the comment on the current file 283 * @param commentBufferSize number of bytes available in szComment 284 * @return EXTRACTOR_UNZIP_OK if there is no problem. 285 */ 286 int 287 EXTRACTOR_common_unzip_get_current_file_info (struct EXTRACTOR_UnzipFile *file, 288 struct EXTRACTOR_UnzipFileInfo * 289 pfile_info, 290 char *szFileName, 291 uLong fileNameBufferSize, 292 void *extraField, 293 uLong extraFieldBufferSize, 294 char *szComment, 295 uLong commentBufferSize); 296 297 298 /** 299 * Open for reading data the current file in the zipfile. 300 * 301 * @param file zipfile to manipulate 302 * @return EXTRACTOR_UNZIP_OK on success 303 */ 304 int 305 EXTRACTOR_common_unzip_open_current_file (struct EXTRACTOR_UnzipFile *file); 306 307 308 /** 309 * Read bytes from the current file (must have been opened). 310 * 311 * @param buf contain buffer where data must be copied 312 * @param len the size of buf. 313 * @return the number of byte copied if somes bytes are copied 314 * 0 if the end of file was reached 315 * <0 with error code if there is an error 316 * (EXTRACTOR_UNZIP_ERRNO for IO error, or zLib error for uncompress error) 317 */ 318 ssize_t 319 EXTRACTOR_common_unzip_read_current_file (struct EXTRACTOR_UnzipFile *file, 320 void *buf, 321 size_t len); 322 323 324 /** 325 * Close the file in zip opened with EXTRACTOR_common_unzip_open_current_file. 326 * 327 * @return EXTRACTOR_UNZIP_CRCERROR if all the file was read but the CRC is not good 328 */ 329 int 330 EXTRACTOR_common_unzip_close_current_file (struct EXTRACTOR_UnzipFile *file); 331 332 333 #endif 334 /* LE_COMMON_UNZIP_H */