Annotation of libaitsync/src/zc.c, revision 1.3

1.2       misho       1: /*************************************************************************
                      2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
                      3: *  by Michael Pounov <misho@openbsd-bg.org>
                      4: *
                      5: * $Author: misho $
1.3     ! misho       6: * $Id: zc.c,v 1.2.2.2 2012/07/22 22:09:18 misho Exp $
1.2       misho       7: *
                      8: **************************************************************************
                      9: The ELWIX and AITNET software is distributed under the following
                     10: terms:
                     11: 
                     12: All of the documentation and software included in the ELWIX and AITNET
                     13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
                     14: 
1.3     ! misho      15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
1.2       misho      16:        by Michael Pounov <misho@elwix.org>.  All rights reserved.
                     17: 
                     18: Redistribution and use in source and binary forms, with or without
                     19: modification, are permitted provided that the following conditions
                     20: are met:
                     21: 1. Redistributions of source code must retain the above copyright
                     22:    notice, this list of conditions and the following disclaimer.
                     23: 2. Redistributions in binary form must reproduce the above copyright
                     24:    notice, this list of conditions and the following disclaimer in the
                     25:    documentation and/or other materials provided with the distribution.
                     26: 3. All advertising materials mentioning features or use of this software
                     27:    must display the following acknowledgement:
                     28: This product includes software developed by Michael Pounov <misho@elwix.org>
                     29: ELWIX - Embedded LightWeight unIX and its contributors.
                     30: 4. Neither the name of AITNET nor the names of its contributors
                     31:    may be used to endorse or promote products derived from this software
                     32:    without specific prior written permission.
                     33: 
                     34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
                     35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     44: SUCH DAMAGE.
                     45: */
1.1       misho      46: #include "global.h"
                     47: #include "file.h"
                     48: #include "zc.h"
                     49: 
                     50: 
1.3     ! misho      51: /*
        !            52:  * sync_Deflate() LibZ deflate data
        !            53:  * @inf = Input file
        !            54:  * @outf = Output file
        !            55:  * @level = Compress level
        !            56:  * return: -1 error, != -1 ok
        !            57: */
        !            58: int
        !            59: sync_Deflate(int inf, int outf, int level)
1.1       misho      60: {
                     61:        z_stream z;
                     62:        int ret, flush, readed;
                     63:        u_char ibuf[Z_CHUNK], obuf[Z_CHUNK];
                     64: 
                     65:        lseek(outf, 0l, SEEK_SET);
                     66:        if (lseek(inf, 0l, SEEK_SET) == -1) {
                     67:                SETERR;
                     68:                return -1;
                     69:        }
                     70: 
                     71:        // init zlib
                     72:        memset(&z, 0, sizeof z);
                     73:        ret = deflateInit(&z, level);
                     74:        if (ret)
                     75:                return ret;
                     76: 
                     77:        do {
                     78:                // set input buffer for compress
                     79:                readed = read(inf, ibuf, Z_CHUNK);
                     80:                if (-1 == readed) {
                     81:                        SETERR;
                     82:                        break;
                     83:                } else {
                     84:                        z.avail_in = readed;
                     85:                        z.next_in = ibuf;
                     86:                }
                     87: 
                     88:                // check flush flag
                     89:                flush = syncEOF(inf) ? Z_FINISH : Z_NO_FLUSH;
                     90: 
                     91:                do {
                     92:                        // compress and write to file
                     93:                        z.avail_out = readed;
                     94:                        z.next_out = obuf;
                     95:                        ret = deflate(&z, flush);
                     96:                        if (Z_STREAM_ERROR == ret) {
                     97:                                syncSetErr(ENOEXEC, "Error:: can`t deflate !!!\n");
                     98:                                flush = Z_FINISH;
                     99:                                ret = -1;
                    100:                                break;
                    101:                        }
                    102: 
                    103:                        ret = write(outf, obuf, readed - z.avail_out);
                    104:                        if (-1 == ret || ret != readed - z.avail_out) {
                    105:                                SETERR;
                    106:                                flush = Z_FINISH;
                    107:                                ret = -1;
                    108:                                break;
                    109:                        }
                    110:                } while (!z.avail_out);
                    111: 
                    112:                if (z.avail_in) {
                    113:                        syncSetErr(ENOEXEC, "Error:: can`t get all input for deflate !!!\n");
                    114:                        ret = -1;
                    115:                        break;
                    116:                }
                    117:        } while (flush != Z_FINISH);
                    118: 
                    119:        // free zlib
                    120:        deflateEnd(&z);
                    121:        return ret;
                    122: }
                    123: 
1.3     ! misho     124: /*
        !           125:  * sync_Inflate() LibZ inflate data
        !           126:  * @inf = Input file
        !           127:  * @outf = Output file
        !           128:  * return: -1 error, != -1 ok
        !           129: */
        !           130: int
        !           131: sync_Inflate(int inf, int outf)
1.1       misho     132: {
                    133:        z_stream z;
                    134:        int ret, flush;
                    135:        u_char ibuf[Z_CHUNK], obuf[Z_CHUNK];
                    136: 
                    137:        lseek(outf, 0l, SEEK_SET);
                    138:        if (lseek(inf, 0l, SEEK_SET) == -1) {
                    139:                SETERR;
                    140:                return -1;
                    141:        }
                    142: 
                    143:        // init zlib
                    144:        memset(&z, 0, sizeof z);
                    145:        ret = inflateInit(&z);
                    146:        if (ret)
                    147:                return ret;
                    148: 
                    149:        do {
                    150:                // set input buffer for decompress
                    151:                ret = read(inf, ibuf, Z_CHUNK);
                    152:                if (-1 == ret) {
                    153:                        SETERR;
                    154:                        break;
                    155:                }
                    156:                if (!ret)
                    157:                        break;
                    158:                z.avail_in = ret;
                    159:                z.next_in = ibuf;
                    160: 
                    161:                flush = Z_NO_FLUSH;
                    162: 
                    163:                do {
                    164:                        // decompress and write to file
                    165:                        z.avail_out = Z_CHUNK;
                    166:                        z.next_out = obuf;
                    167:                        ret = inflate(&z, flush);
                    168:                        switch (ret) {
                    169:                                case Z_NEED_DICT:
                    170:                                case Z_DATA_ERROR:
                    171:                                case Z_MEM_ERROR:
                    172:                                        syncSetErr(ENOEXEC, "Error:: can`t inflate !!!\n");
                    173:                                        flush = Z_STREAM_END;
                    174:                                        ret = -1;
                    175:                                        break;
                    176:                        }
                    177: 
                    178:                        ret = write(outf, obuf, Z_CHUNK - z.avail_out);
                    179:                        if (-1 == ret || ret != Z_CHUNK - z.avail_out) {
                    180:                                SETERR;
                    181:                                flush = Z_STREAM_END;
                    182:                                ret = -1;
                    183:                                break;
                    184:                        }
                    185:                } while (!z.avail_out);
                    186:        } while (flush != Z_STREAM_END);
                    187: 
                    188:        // free zlib
                    189:        inflateEnd(&z);
                    190:        return ret;
                    191: }

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