Back to index

php5  5.3.10
zipint.h
Go to the documentation of this file.
00001 #ifndef _HAD_ZIPINT_H
00002 #define _HAD_ZIPINT_H
00003 
00004 /*
00005   zipint.h -- internal declarations.
00006   Copyright (C) 1999-2009 Dieter Baron and Thomas Klausner
00007 
00008   This file is part of libzip, a library to manipulate ZIP archives.
00009   The authors can be contacted at <libzip@nih.at>
00010 
00011   Redistribution and use in source and binary forms, with or without
00012   modification, are permitted provided that the following conditions
00013   are met:
00014   1. Redistributions of source code must retain the above copyright
00015      notice, this list of conditions and the following disclaimer.
00016   2. Redistributions in binary form must reproduce the above copyright
00017      notice, this list of conditions and the following disclaimer in
00018      the documentation and/or other materials provided with the
00019      distribution.
00020   3. The names of the authors may not be used to endorse or promote
00021      products derived from this software without specific prior
00022      written permission.
00023 
00024   THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
00025   OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00026   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00027   ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
00028   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00030   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00031   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
00032   IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00033   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
00034   IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035 */
00036 
00037 #include <zlib.h>
00038 
00039 #include "zip.h"
00040 
00041 #ifdef PHP_WIN32
00042 #include <windows.h>
00043 #include <wchar.h>
00044 #define _zip_rename(s, t)                                      \
00045        (!MoveFileExA((s), (t),                                        \
00046                    MOVEFILE_COPY_ALLOWED|MOVEFILE_REPLACE_EXISTING))
00047 #else
00048 #define _zip_rename  rename
00049 #endif
00050 
00051 #ifndef strcasecmp
00052 # define strcmpi strcasecmp 
00053 #endif
00054 
00055 #ifndef HAVE_FSEEKO
00056 #define fseeko(s, o, w)     (fseek((s), (long int)(o), (w)))
00057 #endif
00058 #ifndef HAVE_FTELLO
00059 #define ftello(s)    ((long)ftell((s)))
00060 #endif
00061 
00062 
00063 
00064 #define CENTRAL_MAGIC "PK\1\2"
00065 #define LOCAL_MAGIC   "PK\3\4"
00066 #define EOCD_MAGIC    "PK\5\6"
00067 #define DATADES_MAGIC "PK\7\8"
00068 #define TORRENT_SIG  "TORRENTZIPPED-"
00069 #define TORRENT_SIG_LEN     14
00070 #define TORRENT_CRC_LEN 8
00071 #define TORRENT_MEM_LEVEL   8
00072 #define CDENTRYSIZE         46u
00073 #define LENTRYSIZE          30
00074 #undef MAXCOMLEN /* defined as 19 on BSD for max command name */
00075 #define MAXCOMLEN        65536
00076 #define EOCDLEN             22
00077 #define CDBUFSIZE       (MAXCOMLEN+EOCDLEN)
00078 #define BUFSIZE             8192
00079 
00080 
00081 
00082 /* state of change of a file in zip archive */
00083 
00084 enum zip_state { ZIP_ST_UNCHANGED, ZIP_ST_DELETED, ZIP_ST_REPLACED,
00085                ZIP_ST_ADDED, ZIP_ST_RENAMED };
00086 
00087 /* constants for struct zip_file's member flags */
00088 
00089 #define ZIP_ZF_EOF   1 /* EOF reached */
00090 #define ZIP_ZF_DECOMP       2 /* decompress data */
00091 #define ZIP_ZF_CRC   4 /* compute and compare CRC */
00092 
00093 /* directory entry: general purpose bit flags */
00094 
00095 #define ZIP_GPBF_ENCRYPTED         0x0001 /* is encrypted */
00096 #define ZIP_GPBF_DATA_DESCRIPTOR   0x0008 /* crc/size after file data */
00097 #define ZIP_GPBF_STRONG_ENCRYPTION 0x0040  /* uses strong encryption */
00098 
00099 /* error information */
00100 
00101 struct zip_error {
00102     int zip_err;     /* libzip error code (ZIP_ER_*) */
00103     int sys_err;     /* copy of errno (E*) or zlib error code */
00104     char *str;              /* string representation or NULL */
00105 };
00106 
00107 /* zip archive, part of API */
00108 
00109 struct zip {
00110     char *zn;               /* file name */
00111     FILE *zp;               /* file */
00112     struct zip_error error; /* error information */
00113 
00114     unsigned int flags;            /* archive global flags */
00115     unsigned int ch_flags;  /* changed archive global flags */
00116 
00117     struct zip_cdir *cdir;  /* central directory */
00118     char *ch_comment;              /* changed archive comment */
00119     int ch_comment_len;            /* length of changed zip archive
00120                              * comment, -1 if unchanged */
00121     int nentry;                    /* number of entries */
00122     int nentry_alloc;              /* number of entries allocated */
00123     struct zip_entry *entry;       /* entries */
00124     int nfile;                     /* number of opened files within archive */
00125     int nfile_alloc;        /* number of files allocated */
00126     struct zip_file **file; /* opened files within archive */
00127 };
00128 
00129 /* file in zip archive, part of API */
00130 
00131 struct zip_file {
00132     struct zip *za;         /* zip archive containing this file */
00133     struct zip_error error; /* error information */
00134     int flags;                     /* -1: eof, >0: error */
00135 
00136     int method;                    /* compression method */
00137     off_t fpos;                    /* position within zip file (fread/fwrite) */
00138     unsigned long bytes_left;      /* number of bytes left to read */
00139     unsigned long cbytes_left;  /* number of bytes of compressed data left */
00140 
00141     unsigned long crc;             /* CRC so far */
00142     unsigned long crc_orig; /* CRC recorded in archive */
00143 
00144     char *buffer;
00145     z_stream *zstr;
00146 };
00147 
00148 /* zip archive directory entry (central or local) */
00149 
00150 struct zip_dirent {
00151     unsigned short version_madeby; /* (c)  version of creator */
00152     unsigned short version_needed; /* (cl) version needed to extract */
00153     unsigned short bitflags;              /* (cl) general purpose bit flag */
00154     unsigned short comp_method;           /* (cl) compression method used */
00155     time_t last_mod;               /* (cl) time of last modification */
00156     unsigned int crc;                     /* (cl) CRC-32 of uncompressed data */
00157     unsigned int comp_size;        /* (cl) size of commpressed data */
00158     unsigned int uncomp_size;             /* (cl) size of uncommpressed data */
00159     char *filename;                /* (cl) file name (NUL-terminated) */
00160     unsigned short filename_len;   /* (cl) length of filename (w/o NUL) */
00161     char *extrafield;                     /* (cl) extra field */
00162     unsigned short extrafield_len; /* (cl) length of extra field */
00163     char *comment;                 /* (c)  file comment */
00164     unsigned short comment_len;           /* (c)  length of file comment */
00165     unsigned short disk_number;           /* (c)  disk number start */
00166     unsigned short int_attrib;            /* (c)  internal file attributes */
00167     unsigned int ext_attrib;              /* (c)  external file attributes */
00168     unsigned int offset;           /* (c)  offset of local header  */
00169 };
00170 
00171 /* zip archive central directory */
00172 
00173 struct zip_cdir {
00174     struct zip_dirent *entry;      /* directory entries */
00175     int nentry;                    /* number of entries */
00176 
00177     unsigned int size;             /* size of central direcotry */
00178     unsigned int offset;    /* offset of central directory in file */
00179     char *comment;          /* zip archive comment */
00180     unsigned short comment_len;    /* length of zip archive comment */
00181 };
00182 
00183 
00184 
00185 struct zip_source {
00186     zip_source_callback f;
00187     void *ud;
00188 };
00189 
00190 /* entry in zip archive directory */
00191 
00192 struct zip_entry {
00193     enum zip_state state;
00194     struct zip_source *source;
00195     char *ch_filename;
00196     char *ch_comment;
00197     int ch_comment_len;
00198 };
00199 
00200 
00201 
00202 extern const char * const _zip_err_str[];
00203 extern const int _zip_nerr_str;
00204 extern const int _zip_err_type[];
00205 
00206 
00207 
00208 #define ZIP_ENTRY_DATA_CHANGED(x)  \
00209                      ((x)->state == ZIP_ST_REPLACED  \
00210                       || (x)->state == ZIP_ST_ADDED)
00211 
00212 
00213 
00214 int _zip_cdir_compute_crc(struct zip *, uLong *);
00215 void _zip_cdir_free(struct zip_cdir *);
00216 int _zip_cdir_grow(struct zip_cdir *, int, struct zip_error *);
00217 struct zip_cdir *_zip_cdir_new(int, struct zip_error *);
00218 int _zip_cdir_write(struct zip_cdir *, FILE *, struct zip_error *);
00219 
00220 void _zip_dirent_finalize(struct zip_dirent *);
00221 void _zip_dirent_init(struct zip_dirent *);
00222 int _zip_dirent_read(struct zip_dirent *, FILE *, unsigned char **,
00223                    unsigned int *, int, struct zip_error *);
00224 void _zip_dirent_torrent_normalize(struct zip_dirent *);
00225 int _zip_dirent_write(struct zip_dirent *, FILE *, int, struct zip_error *);
00226 
00227 void _zip_entry_free(struct zip_entry *);
00228 void _zip_entry_init(struct zip *, int);
00229 struct zip_entry *_zip_entry_new(struct zip *);
00230 
00231 void _zip_error_clear(struct zip_error *);
00232 void _zip_error_copy(struct zip_error *, struct zip_error *);
00233 void _zip_error_fini(struct zip_error *);
00234 void _zip_error_get(struct zip_error *, int *, int *);
00235 void _zip_error_init(struct zip_error *);
00236 void _zip_error_set(struct zip_error *, int, int);
00237 const char *_zip_error_strerror(struct zip_error *);
00238 
00239 int _zip_file_fillbuf(void *, size_t, struct zip_file *);
00240 unsigned int _zip_file_get_offset(struct zip *, int);
00241 
00242 int _zip_filerange_crc(FILE *, off_t, off_t, uLong *, struct zip_error *);
00243 
00244 struct zip_source *_zip_source_file_or_p(struct zip *, const char *, FILE *,
00245                                     off_t, off_t);
00246 
00247 void _zip_free(struct zip *);
00248 const char *_zip_get_name(struct zip *, int, int, struct zip_error *);
00249 int _zip_local_header_read(struct zip *, int);
00250 void *_zip_memdup(const void *, size_t, struct zip_error *);
00251 int _zip_name_locate(struct zip *, const char *, int, struct zip_error *);
00252 struct zip *_zip_new(struct zip_error *);
00253 unsigned short _zip_read2(unsigned char **);
00254 unsigned int _zip_read4(unsigned char **);
00255 int _zip_replace(struct zip *, int, const char *, struct zip_source *);
00256 int _zip_set_name(struct zip *, int, const char *);
00257 int _zip_unchange(struct zip *, int, int);
00258 void _zip_unchange_data(struct zip_entry *);
00259 
00260 #endif /* zipint.h */