libextractor

GNU libextractor
Log | Files | Refs | Submodules | README | LICENSE

unzip.h (8793B)


      1 /*
      2      This file is part of libextractor.
      3      Copyright (C) 2008, 2012, 2026 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 Franlkin 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 <stdint.h>
     33 #include <stddef.h>
     34 #include <sys/types.h>
     35 #include <zlib.h>       /* for Z_ERRNO */
     36 
     37 /**
     38  * Operation was successful.
     39  */
     40 #define EXTRACTOR_UNZIP_OK                          (0)
     41 
     42 /**
     43  * Cannot move to next file, we are at the end
     44  */
     45 #define EXTRACTOR_UNZIP_END_OF_LIST_OF_FILE         (-100)
     46 
     47 /**
     48  * IO error, see errno.
     49  */
     50 #define EXTRACTOR_UNZIP_ERRNO                       (Z_ERRNO)
     51 
     52 /**
     53  * Reached end of the file (NOTE: same as OK!)
     54  *
     55  * Because this is numerically #EXTRACTOR_UNZIP_OK, it can only ever be
     56  * used where "nothing left to read" is a *success*, i.e. as the 0 return
     57  * of #EXTRACTOR_common_unzip_read_current_file().  It must never be
     58  * returned to signal a short or failed read: the caller's
     59  * `EXTRACTOR_UNZIP_OK != err` test cannot tell the two apart and would
     60  * go on to use data that was never written.
     61  */
     62 #define EXTRACTOR_UNZIP_EOF                         (0)
     63 
     64 /**
     65  * Invalid arguments to call.
     66  */
     67 #define EXTRACTOR_UNZIP_PARAMERROR                  (-102)
     68 
     69 /**
     70  * Not a zip file (or malformed)
     71  */
     72 #define EXTRACTOR_UNZIP_BADZIPFILE                  (-103)
     73 
     74 /**
     75  * Internal error.
     76  */
     77 #define EXTRACTOR_UNZIP_INTERNALERROR               (-104)
     78 
     79 /**
     80  * Checksum failure.
     81  */
     82 #define EXTRACTOR_UNZIP_CRCERROR                    (-105)
     83 
     84 /**
     85  * Handle for a ZIP archive.
     86  */
     87 struct EXTRACTOR_UnzipFile;
     88 
     89 
     90 /**
     91  * date/time information
     92  */
     93 struct EXTRACTOR_UnzipDateTimeInfo
     94 {
     95   /**
     96    * seconds after the minute - [0,59]
     97    */
     98   unsigned int tm_sec;
     99 
    100   /**
    101    * minutes after the hour - [0,59]
    102    */
    103   unsigned int tm_min;
    104 
    105   /**
    106    * hours since midnight - [0,23]
    107    */
    108   unsigned int tm_hour;
    109 
    110   /**
    111    * day of the month - [1,31]
    112    */
    113   unsigned int tm_mday;
    114 
    115   /**
    116    * months since January - [0,11]
    117    */
    118   unsigned int tm_mon;
    119 
    120   /**
    121    * years - [1980..2044]
    122    */
    123   unsigned int tm_year;
    124 };
    125 
    126 
    127 /**
    128  * Information about a file in the zipfile.
    129  *
    130  * Every member mirrors a field of the ZIP central directory header and
    131  * has exactly that field's width, so a value that the format cannot
    132  * represent cannot be reported here either.
    133  */
    134 struct EXTRACTOR_UnzipFileInfo
    135 {
    136   /**
    137    * version made by                 2 bytes
    138    */
    139   uint16_t version;
    140 
    141   /**
    142    * version needed to extract       2 bytes
    143    */
    144   uint16_t version_needed;
    145 
    146   /**
    147    * general purpose bit flag        2 bytes
    148    */
    149   uint16_t flag;
    150 
    151   /**
    152    * compression method              2 bytes
    153    */
    154   uint16_t compression_method;
    155 
    156   /**
    157    * last mod file date in Dos fmt   4 bytes
    158    */
    159   uint32_t dosDate;
    160 
    161   /**
    162    * crc-32                          4 bytes
    163    */
    164   uint32_t crc;
    165 
    166   /**
    167    * compressed size                 4 bytes
    168    */
    169   uint32_t compressed_size;
    170 
    171   /**
    172    * uncompressed size               4 bytes
    173    */
    174   uint32_t uncompressed_size;
    175 
    176   /**
    177    * filename length                 2 bytes
    178    */
    179   uint16_t size_filename;
    180 
    181   /**
    182    * extra field length              2 bytes
    183    */
    184   uint16_t size_file_extra;
    185 
    186   /**
    187    * file comment length             2 bytes
    188    */
    189   uint16_t size_file_comment;
    190 
    191   /**
    192    * disk number start               2 bytes
    193    */
    194   uint16_t disk_num_start;
    195 
    196   /**
    197    * internal file attributes        2 bytes
    198    */
    199   uint16_t internal_fa;
    200 
    201   /**
    202    * external file attributes        4 bytes
    203    */
    204   uint32_t external_fa;
    205 
    206   /**
    207    * Time and date of last modification.
    208    */
    209   struct EXTRACTOR_UnzipDateTimeInfo tmu_date;
    210 };
    211 
    212 
    213 /**
    214  * Open a zip file for processing using the data access
    215  * functions from the extract context.
    216  *
    217  * @param ec extract context to use
    218  * @return handle to zip data, NULL on error
    219  */
    220 struct EXTRACTOR_UnzipFile *
    221 EXTRACTOR_common_unzip_open (struct EXTRACTOR_ExtractContext *ec);
    222 
    223 
    224 /**
    225  * Obtain the global comment from a ZIP file.
    226  *
    227  * @param file unzip file to inspect
    228  * @param comment where to copy the comment
    229  * @param comment_len maximum number of bytes available in comment
    230  * @return EXTRACTOR_UNZIP_OK on success
    231  */
    232 int
    233 EXTRACTOR_common_unzip_get_global_comment (struct EXTRACTOR_UnzipFile *file,
    234                                            char *comment,
    235                                            size_t comment_len);
    236 
    237 
    238 /**
    239  * Close a ZipFile.
    240  *
    241  * @param file zip file to close
    242  * @return EXTRACTOR_UNZIP_OK if there is no problem.
    243  */
    244 int
    245 EXTRACTOR_common_unzip_close (struct EXTRACTOR_UnzipFile *file);
    246 
    247 
    248 /**
    249  * Set the current file of the zipfile to the first file.
    250  *
    251  * @param file zipfile to manipulate
    252  * @return UNZ_OK if there is no problem
    253  */
    254 int
    255 EXTRACTOR_common_unzip_go_to_first_file (struct EXTRACTOR_UnzipFile *file);
    256 
    257 
    258 /**
    259  * Set the current file of the zipfile to the next file.
    260  *
    261  * @param file zipfile to manipulate
    262  * @return EXTRACTOR_UNZIP_OK if there is no problem,
    263  *         EXTRACTOR_UNZIP_END_OF_LIST_OF_FILE if the actual file was the latest.
    264  */
    265 int
    266 EXTRACTOR_common_unzip_go_to_next_file (struct EXTRACTOR_UnzipFile *file);
    267 
    268 
    269 /**
    270  * Try locate the file szFileName in the zipfile.
    271  *
    272  * @param file zipfile to manipulate
    273  * @param szFileName name to find
    274  * @param iCaseSensitivity, use 1 for case sensitivity (like strcmp);
    275  *        2 for no case sensitivity (like strcmpi or strcasecmp); or
    276  *        0 for defaut of your operating system (like 1 on Unix, 2 on Windows)
    277  * @return EXTRACTOR_UNZIP_OK if the file is found. It becomes the current file.
    278  *         EXTRACTOR_UNZIP_END_OF_LIST_OF_FILE if the file is not found
    279  */
    280 int
    281 EXTRACTOR_common_unzip_go_find_local_file (struct EXTRACTOR_UnzipFile *file,
    282                                            const char *szFileName,
    283                                            int iCaseSensitivity);
    284 
    285 
    286 /**
    287  * Write info about the ZipFile in the *pglobal_info structure.
    288  * No preparation of the structure is needed.
    289  *
    290  * @param file zipfile to manipulate
    291  * @param pfile_info file information to initialize
    292  * @param szFileName where to write the name of the current file
    293  * @param fileNameBufferSize number of bytes available in szFileName
    294  * @param extraField where to write extra data
    295  * @param extraFieldBufferSize number of bytes available in extraField
    296  * @param szComment where to write the comment on the current file
    297  * @param commentBufferSize number of bytes available in szComment
    298  * @return EXTRACTOR_UNZIP_OK if there is no problem.
    299  */
    300 int
    301 EXTRACTOR_common_unzip_get_current_file_info (
    302   struct EXTRACTOR_UnzipFile *file,
    303   struct EXTRACTOR_UnzipFileInfo *pfile_info,
    304   char *szFileName,
    305   size_t fileNameBufferSize,
    306   void *extraField,
    307   size_t extraFieldBufferSize,
    308   char *szComment,
    309   size_t commentBufferSize);
    310 
    311 
    312 /**
    313  * Open for reading data the current file in the zipfile.
    314  *
    315  * @param file zipfile to manipulate
    316  * @return #EXTRACTOR_UNZIP_OK on success
    317  */
    318 int
    319 EXTRACTOR_common_unzip_open_current_file (struct EXTRACTOR_UnzipFile *file);
    320 
    321 
    322 /**
    323  * Read bytes from the current file (must have been opened).
    324  *
    325  * @param buf contain buffer where data must be copied
    326  * @param len the size of buf.
    327  * @return the number of byte copied if somes bytes are copied
    328  *         0 if the end of file was reached
    329  *         <0 with error code if there is an error
    330  *        (EXTRACTOR_UNZIP_ERRNO for IO error, or zLib error for uncompress error)
    331  */
    332 ssize_t
    333 EXTRACTOR_common_unzip_read_current_file (struct EXTRACTOR_UnzipFile *file,
    334                                           void *buf,
    335                                           size_t len);
    336 
    337 
    338 /**
    339  * Close the file in zip opened with EXTRACTOR_common_unzip_open_current_file.
    340  *
    341  * @return #EXTRACTOR_UNZIP_CRCERROR if all the file was read but the CRC is not good
    342  */
    343 int
    344 EXTRACTOR_common_unzip_close_current_file (struct EXTRACTOR_UnzipFile *file);
    345 
    346 
    347 #endif
    348 /* LE_COMMON_UNZIP_H */