Back to index

php5  5.3.10
zip_source_zip.c
Go to the documentation of this file.
00001 /*
00002   zip_source_zip.c -- create data source from zip file
00003   Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
00004 
00005   This file is part of libzip, a library to manipulate ZIP archives.
00006   The authors can be contacted at <libzip@nih.at>
00007 
00008   Redistribution and use in source and binary forms, with or without
00009   modification, are permitted provided that the following conditions
00010   are met:
00011   1. Redistributions of source code must retain the above copyright
00012      notice, this list of conditions and the following disclaimer.
00013   2. Redistributions in binary form must reproduce the above copyright
00014      notice, this list of conditions and the following disclaimer in
00015      the documentation and/or other materials provided with the
00016      distribution.
00017   3. The names of the authors may not be used to endorse or promote
00018      products derived from this software without specific prior
00019      written permission.
00020  
00021   THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
00022   OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00023   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00024   ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
00025   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00026   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00027   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00028   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
00029   IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00030   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
00031   IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032 */
00033 
00034 
00035 
00036 #include <stdlib.h>
00037 #include <string.h>
00038 
00039 #include "zipint.h"
00040 
00041 struct read_zip {
00042     struct zip_file *zf;
00043     struct zip_stat st;
00044     off_t off, len;
00045 };
00046 
00047 static ssize_t read_zip(void *st, void *data, size_t len,
00048                      enum zip_source_cmd cmd);
00049 
00050 
00051 
00052 ZIP_EXTERN(struct zip_source *)
00053 zip_source_zip(struct zip *za, struct zip *srcza, int srcidx, int flags,
00054               off_t start, off_t len)
00055 {
00056     struct zip_error error;
00057     struct zip_source *zs;
00058     struct read_zip *p;
00059 
00060     /* XXX: ZIP_FL_RECOMPRESS */
00061 
00062     if (za == NULL)
00063        return NULL;
00064 
00065     if (srcza == NULL || start < 0 || len < -1 || srcidx < 0 || srcidx >= srcza->nentry) {
00066        _zip_error_set(&za->error, ZIP_ER_INVAL, 0);
00067        return NULL;
00068     }
00069 
00070     if ((flags & ZIP_FL_UNCHANGED) == 0
00071        && ZIP_ENTRY_DATA_CHANGED(srcza->entry+srcidx)) {
00072        _zip_error_set(&za->error, ZIP_ER_CHANGED, 0);
00073        return NULL;
00074     }
00075 
00076     if (len == 0)
00077        len = -1;
00078 
00079     if (start == 0 && len == -1 && (flags & ZIP_FL_RECOMPRESS) == 0)
00080        flags |= ZIP_FL_COMPRESSED;
00081     else
00082        flags &= ~ZIP_FL_COMPRESSED;
00083 
00084     if ((p=(struct read_zip *)malloc(sizeof(*p))) == NULL) {
00085        _zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
00086        return NULL;
00087     }
00088        
00089     _zip_error_copy(&error, &srcza->error);
00090        
00091     if (zip_stat_index(srcza, srcidx, flags, &p->st) < 0
00092        || (p->zf=zip_fopen_index(srcza, srcidx, flags)) == NULL) {
00093        free(p);
00094        _zip_error_copy(&za->error, &srcza->error);
00095        _zip_error_copy(&srcza->error, &error);
00096        
00097        return NULL;
00098     }
00099     p->off = start;
00100     p->len = len;
00101 
00102     if ((flags & ZIP_FL_COMPRESSED) == 0) {
00103        p->st.size = p->st.comp_size = len;
00104        p->st.comp_method = ZIP_CM_STORE;
00105        p->st.crc = 0;
00106     }
00107     
00108     if ((zs=zip_source_function(za, read_zip, p)) == NULL) {
00109        free(p);
00110        return NULL;
00111     }
00112 
00113     return zs;
00114 }
00115 
00116 
00117 
00118 static ssize_t
00119 read_zip(void *state, void *data, size_t len, enum zip_source_cmd cmd)
00120 {
00121     struct read_zip *z;
00122     char b[8192], *buf;
00123     int i, n;
00124 
00125     z = (struct read_zip *)state;
00126     buf = (char *)data;
00127 
00128     switch (cmd) {
00129     case ZIP_SOURCE_OPEN:
00130        for (n=0; n<z->off; n+= i) {
00131            i = (z->off-n > sizeof(b) ? sizeof(b) : z->off-n);
00132            if ((i=zip_fread(z->zf, b, i)) < 0) {
00133               zip_fclose(z->zf);
00134               z->zf = NULL;
00135               return -1;
00136            }
00137        }
00138        return 0;
00139        
00140     case ZIP_SOURCE_READ:
00141        if (z->len != -1)
00142            n = len > z->len ? z->len : len;
00143        else
00144            n = len;
00145        
00146 
00147        if ((i=zip_fread(z->zf, buf, n)) < 0)
00148            return -1;
00149 
00150        if (z->len != -1)
00151            z->len -= i;
00152 
00153        return i;
00154        
00155     case ZIP_SOURCE_CLOSE:
00156        return 0;
00157 
00158     case ZIP_SOURCE_STAT:
00159        if (len < sizeof(z->st))
00160            return -1;
00161        len = sizeof(z->st);
00162 
00163        memcpy(data, &z->st, len);
00164        return len;
00165 
00166     case ZIP_SOURCE_ERROR:
00167        {
00168            int *e;
00169 
00170            if (len < sizeof(int)*2)
00171               return -1;
00172 
00173            e = (int *)data;
00174            zip_file_error_get(z->zf, e, e+1);
00175        }
00176        return sizeof(int)*2;
00177 
00178     case ZIP_SOURCE_FREE:
00179        zip_fclose(z->zf);
00180        free(z);
00181        return 0;
00182 
00183     default:
00184        ;
00185     }
00186 
00187     return -1;
00188 }