Back to index

php5  5.3.10
zip_fread.c
Go to the documentation of this file.
00001 /*
00002   zip_fread.c -- read from 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 "zipint.h"
00037 
00038 
00039 
00040 ZIP_EXTERN(ssize_t)
00041 zip_fread(struct zip_file *zf, void *outbuf, size_t toread)
00042 {
00043     int ret;
00044        size_t out_before, len;
00045     int i;
00046 
00047     if (!zf)
00048        return -1;
00049 
00050     if (zf->error.zip_err != 0)
00051        return -1;
00052 
00053     if ((zf->flags & ZIP_ZF_EOF) || (toread == 0))
00054        return 0;
00055 
00056     if (zf->bytes_left == 0) {
00057        zf->flags |= ZIP_ZF_EOF;
00058        if (zf->flags & ZIP_ZF_CRC) {
00059            if (zf->crc != zf->crc_orig) {
00060               _zip_error_set(&zf->error, ZIP_ER_CRC, 0);
00061               return -1;
00062            }
00063        }
00064        return 0;
00065     }
00066 
00067     if ((zf->flags & ZIP_ZF_DECOMP) == 0) {
00068        ret = _zip_file_fillbuf(outbuf, toread, zf);
00069        if (ret > 0) {
00070            if (zf->flags & ZIP_ZF_CRC)
00071               zf->crc = crc32(zf->crc, (Bytef *)outbuf, ret);
00072            zf->bytes_left -= ret;
00073        }
00074        return ret;
00075     }
00076     
00077     zf->zstr->next_out = (Bytef *)outbuf;
00078     zf->zstr->avail_out = toread;
00079     out_before = zf->zstr->total_out;
00080     
00081     /* endless loop until something has been accomplished */
00082     for (;;) {
00083        ret = inflate(zf->zstr, Z_SYNC_FLUSH);
00084 
00085        switch (ret) {
00086        case Z_STREAM_END:
00087            if (zf->zstr->total_out == out_before) {
00088               if (zf->crc != zf->crc_orig) {
00089                   _zip_error_set(&zf->error, ZIP_ER_CRC, 0);
00090                   return -1;
00091               }
00092               else
00093                   return 0;
00094            }
00095 
00096            /* fallthrough */
00097 
00098        case Z_OK:
00099            len = zf->zstr->total_out - out_before;
00100            if (len >= zf->bytes_left || len >= toread) {
00101               if (zf->flags & ZIP_ZF_CRC)
00102                   zf->crc = crc32(zf->crc, (Bytef *)outbuf, len);
00103               zf->bytes_left -= len;
00104                return len;
00105            }
00106            break;
00107 
00108        case Z_BUF_ERROR:
00109            if (zf->zstr->avail_in == 0) {
00110               i = _zip_file_fillbuf(zf->buffer, BUFSIZE, zf);
00111               if (i == 0) {
00112                   _zip_error_set(&zf->error, ZIP_ER_INCONS, 0);
00113                   return -1;
00114               }
00115               else if (i < 0)
00116                   return -1;
00117               zf->zstr->next_in = (Bytef *)zf->buffer;
00118               zf->zstr->avail_in = i;
00119               continue;
00120            }
00121            /* fallthrough */
00122        case Z_NEED_DICT:
00123        case Z_DATA_ERROR:
00124        case Z_STREAM_ERROR:
00125        case Z_MEM_ERROR:
00126            _zip_error_set(&zf->error, ZIP_ER_ZLIB, ret);
00127            return -1;
00128        }
00129     }
00130 }