Back to index

php5  5.3.10
zip_fopen_index.c
Go to the documentation of this file.
00001 /*
00002   zip_fopen_index.c -- open file in zip archive for reading by index
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 <errno.h>
00037 #include <stdio.h>
00038 #include <stdlib.h>
00039 
00040 #include "zipint.h"
00041 
00042 static struct zip_file *_zip_file_new(struct zip *za);
00043 
00044 
00045 
00046 ZIP_EXTERN(struct zip_file *)
00047 zip_fopen_index(struct zip *za, int fileno, int flags)
00048 {
00049     int len, ret;
00050     int zfflags;
00051     struct zip_file *zf;
00052 
00053     if ((fileno < 0) || (fileno >= za->nentry)) {
00054        _zip_error_set(&za->error, ZIP_ER_INVAL, 0);
00055        return NULL;
00056     }
00057 
00058     if ((flags & ZIP_FL_UNCHANGED) == 0
00059        && ZIP_ENTRY_DATA_CHANGED(za->entry+fileno)) {
00060        _zip_error_set(&za->error, ZIP_ER_CHANGED, 0);
00061        return NULL;
00062     }
00063 
00064     if (fileno >= za->cdir->nentry) {
00065        _zip_error_set(&za->error, ZIP_ER_INVAL, 0);
00066        return NULL;
00067     }
00068 
00069     zfflags = 0;
00070     switch (za->cdir->entry[fileno].comp_method) {
00071     case ZIP_CM_STORE:
00072        zfflags |= ZIP_ZF_CRC;
00073        break;
00074 
00075     case ZIP_CM_DEFLATE:
00076        if ((flags & ZIP_FL_COMPRESSED) == 0)
00077            zfflags |= ZIP_ZF_CRC | ZIP_ZF_DECOMP;
00078        break;
00079     default:
00080        if ((flags & ZIP_FL_COMPRESSED) == 0) {
00081            _zip_error_set(&za->error, ZIP_ER_COMPNOTSUPP, 0);
00082            return NULL;
00083        }
00084        break;
00085     }
00086 
00087     zf = _zip_file_new(za);
00088 
00089     zf->flags = zfflags;
00090     /* zf->name = za->cdir->entry[fileno].filename; */
00091     zf->method = za->cdir->entry[fileno].comp_method;
00092     zf->bytes_left = za->cdir->entry[fileno].uncomp_size;
00093     zf->cbytes_left = za->cdir->entry[fileno].comp_size;
00094     zf->crc_orig = za->cdir->entry[fileno].crc;
00095 
00096     if ((zf->fpos=_zip_file_get_offset(za, fileno)) == 0) {
00097        zip_fclose(zf);
00098        return NULL;
00099     }
00100     
00101     if ((zf->flags & ZIP_ZF_DECOMP) == 0)
00102        zf->bytes_left = zf->cbytes_left;
00103     else {
00104        if ((zf->buffer=(char *)malloc(BUFSIZE)) == NULL) {
00105            _zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
00106            zip_fclose(zf);
00107            return NULL;
00108        }
00109 
00110        len = _zip_file_fillbuf(zf->buffer, BUFSIZE, zf);
00111        if (len <= 0) {
00112            _zip_error_copy(&za->error, &zf->error);
00113            zip_fclose(zf);
00114        return NULL;
00115        }
00116 
00117        if ((zf->zstr = (z_stream *)malloc(sizeof(z_stream))) == NULL) {
00118            _zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
00119            zip_fclose(zf);
00120            return NULL;
00121        }
00122        zf->zstr->zalloc = Z_NULL;
00123        zf->zstr->zfree = Z_NULL;
00124        zf->zstr->opaque = NULL;
00125        zf->zstr->next_in = (Bytef *)zf->buffer;
00126        zf->zstr->avail_in = len;
00127        
00128        /* negative value to tell zlib that there is no header */
00129        if ((ret=inflateInit2(zf->zstr, -MAX_WBITS)) != Z_OK) {
00130            _zip_error_set(&za->error, ZIP_ER_ZLIB, ret);
00131            zip_fclose(zf);
00132            return NULL;
00133        }
00134     }
00135     
00136     return zf;
00137 }
00138 
00139 
00140 
00141 int
00142 _zip_file_fillbuf(void *buf, size_t buflen, struct zip_file *zf)
00143 {
00144     int i, j;
00145 
00146     if (zf->error.zip_err != ZIP_ER_OK)
00147        return -1;
00148 
00149     if ((zf->flags & ZIP_ZF_EOF) || zf->cbytes_left <= 0 || buflen <= 0)
00150        return 0;
00151     
00152     if (fseeko(zf->za->zp, zf->fpos, SEEK_SET) < 0) {
00153        _zip_error_set(&zf->error, ZIP_ER_SEEK, errno);
00154        return -1;
00155     }
00156     if (buflen < zf->cbytes_left)
00157        i = buflen;
00158     else
00159        i = zf->cbytes_left;
00160 
00161     j = fread(buf, 1, i, zf->za->zp);
00162     if (j == 0) {
00163        _zip_error_set(&zf->error, ZIP_ER_EOF, 0);
00164        j = -1;
00165     }
00166     else if (j < 0)
00167        _zip_error_set(&zf->error, ZIP_ER_READ, errno);
00168     else {
00169        zf->fpos += j;
00170        zf->cbytes_left -= j;
00171     }
00172 
00173     return j; 
00174 }
00175 
00176 
00177 
00178 static struct zip_file *
00179 _zip_file_new(struct zip *za)
00180 {
00181     struct zip_file *zf, **file;
00182     int n;
00183 
00184     if ((zf=(struct zip_file *)malloc(sizeof(struct zip_file))) == NULL) {
00185        _zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
00186        return NULL;
00187     }
00188     
00189     if (za->nfile >= za->nfile_alloc-1) {
00190        n = za->nfile_alloc + 10;
00191        file = (struct zip_file **)realloc(za->file,
00192                                       n*sizeof(struct zip_file *));
00193        if (file == NULL) {
00194            _zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
00195            free(zf);
00196            return NULL;
00197        }
00198        za->nfile_alloc = n;
00199        za->file = file;
00200     }
00201 
00202     za->file[za->nfile++] = zf;
00203 
00204     zf->za = za;
00205     _zip_error_init(&zf->error);
00206     zf->flags = 0;
00207     zf->crc = crc32(0L, Z_NULL, 0);
00208     zf->crc_orig = 0;
00209     zf->method = -1;
00210     zf->bytes_left = zf->cbytes_left = 0;
00211     zf->fpos = 0;
00212     zf->buffer = NULL;
00213     zf->zstr = NULL;
00214 
00215     return zf;
00216 }