Annotation of embedaddon/php/ext/zip/lib/zip_fread.c, revision 1.1

1.1     ! misho       1: /*
        !             2:   zip_fread.c -- read from file
        !             3:   Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
        !             4: 
        !             5:   This file is part of libzip, a library to manipulate ZIP archives.
        !             6:   The authors can be contacted at <libzip@nih.at>
        !             7: 
        !             8:   Redistribution and use in source and binary forms, with or without
        !             9:   modification, are permitted provided that the following conditions
        !            10:   are met:
        !            11:   1. Redistributions of source code must retain the above copyright
        !            12:      notice, this list of conditions and the following disclaimer.
        !            13:   2. Redistributions in binary form must reproduce the above copyright
        !            14:      notice, this list of conditions and the following disclaimer in
        !            15:      the documentation and/or other materials provided with the
        !            16:      distribution.
        !            17:   3. The names of the authors may not be used to endorse or promote
        !            18:      products derived from this software without specific prior
        !            19:      written permission.
        !            20:  
        !            21:   THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
        !            22:   OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
        !            23:   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
        !            24:   ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
        !            25:   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        !            26:   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
        !            27:   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
        !            28:   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
        !            29:   IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
        !            30:   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
        !            31:   IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
        !            32: */
        !            33: 
        !            34: 
        !            35: 
        !            36: #include "zipint.h"
        !            37: 
        !            38: 
        !            39: 
        !            40: ZIP_EXTERN(ssize_t)
        !            41: zip_fread(struct zip_file *zf, void *outbuf, size_t toread)
        !            42: {
        !            43:     int ret;
        !            44:        size_t out_before, len;
        !            45:     int i;
        !            46: 
        !            47:     if (!zf)
        !            48:        return -1;
        !            49: 
        !            50:     if (zf->error.zip_err != 0)
        !            51:        return -1;
        !            52: 
        !            53:     if ((zf->flags & ZIP_ZF_EOF) || (toread == 0))
        !            54:        return 0;
        !            55: 
        !            56:     if (zf->bytes_left == 0) {
        !            57:        zf->flags |= ZIP_ZF_EOF;
        !            58:        if (zf->flags & ZIP_ZF_CRC) {
        !            59:            if (zf->crc != zf->crc_orig) {
        !            60:                _zip_error_set(&zf->error, ZIP_ER_CRC, 0);
        !            61:                return -1;
        !            62:            }
        !            63:        }
        !            64:        return 0;
        !            65:     }
        !            66: 
        !            67:     if ((zf->flags & ZIP_ZF_DECOMP) == 0) {
        !            68:        ret = _zip_file_fillbuf(outbuf, toread, zf);
        !            69:        if (ret > 0) {
        !            70:            if (zf->flags & ZIP_ZF_CRC)
        !            71:                zf->crc = crc32(zf->crc, (Bytef *)outbuf, ret);
        !            72:            zf->bytes_left -= ret;
        !            73:        }
        !            74:        return ret;
        !            75:     }
        !            76:     
        !            77:     zf->zstr->next_out = (Bytef *)outbuf;
        !            78:     zf->zstr->avail_out = toread;
        !            79:     out_before = zf->zstr->total_out;
        !            80:     
        !            81:     /* endless loop until something has been accomplished */
        !            82:     for (;;) {
        !            83:        ret = inflate(zf->zstr, Z_SYNC_FLUSH);
        !            84: 
        !            85:        switch (ret) {
        !            86:        case Z_STREAM_END:
        !            87:            if (zf->zstr->total_out == out_before) {
        !            88:                if (zf->crc != zf->crc_orig) {
        !            89:                    _zip_error_set(&zf->error, ZIP_ER_CRC, 0);
        !            90:                    return -1;
        !            91:                }
        !            92:                else
        !            93:                    return 0;
        !            94:            }
        !            95: 
        !            96:            /* fallthrough */
        !            97: 
        !            98:        case Z_OK:
        !            99:            len = zf->zstr->total_out - out_before;
        !           100:            if (len >= zf->bytes_left || len >= toread) {
        !           101:                if (zf->flags & ZIP_ZF_CRC)
        !           102:                    zf->crc = crc32(zf->crc, (Bytef *)outbuf, len);
        !           103:                zf->bytes_left -= len;
        !           104:                return len;
        !           105:            }
        !           106:            break;
        !           107: 
        !           108:        case Z_BUF_ERROR:
        !           109:            if (zf->zstr->avail_in == 0) {
        !           110:                i = _zip_file_fillbuf(zf->buffer, BUFSIZE, zf);
        !           111:                if (i == 0) {
        !           112:                    _zip_error_set(&zf->error, ZIP_ER_INCONS, 0);
        !           113:                    return -1;
        !           114:                }
        !           115:                else if (i < 0)
        !           116:                    return -1;
        !           117:                zf->zstr->next_in = (Bytef *)zf->buffer;
        !           118:                zf->zstr->avail_in = i;
        !           119:                continue;
        !           120:            }
        !           121:            /* fallthrough */
        !           122:        case Z_NEED_DICT:
        !           123:        case Z_DATA_ERROR:
        !           124:        case Z_STREAM_ERROR:
        !           125:        case Z_MEM_ERROR:
        !           126:            _zip_error_set(&zf->error, ZIP_ER_ZLIB, ret);
        !           127:            return -1;
        !           128:        }
        !           129:     }
        !           130: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>