File:  [ELWIX - Embedded LightWeight unIX -] / libaitsync / src / zc.c
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Mon May 9 14:36:33 2011 UTC (13 years, 2 months ago) by misho
Branches: MAIN
CVS tags: sync1_1, SYNC1_0, HEAD
ver 1.0

    1: /*************************************************************************
    2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
    3: *  by Michael Pounov <misho@openbsd-bg.org>
    4: *
    5: * $Author: misho $
    6: * $Id: zc.c,v 1.2 2011/05/09 14:36:33 misho Exp $
    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: 
   15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
   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: */
   46: #include "global.h"
   47: #include "file.h"
   48: #include "zc.h"
   49: 
   50: 
   51: int sync_Deflate(int inf, int outf, int level)
   52: {
   53: 	z_stream z;
   54: 	int ret, flush, readed;
   55: 	u_char ibuf[Z_CHUNK], obuf[Z_CHUNK];
   56: 
   57: 	lseek(outf, 0l, SEEK_SET);
   58: 	if (lseek(inf, 0l, SEEK_SET) == -1) {
   59: 		SETERR;
   60: 		return -1;
   61: 	}
   62: 
   63: 	// init zlib
   64: 	memset(&z, 0, sizeof z);
   65: 	ret = deflateInit(&z, level);
   66: 	if (ret)
   67: 		return ret;
   68: 
   69: 	do {
   70: 		// set input buffer for compress
   71: 		readed = read(inf, ibuf, Z_CHUNK);
   72: 		if (-1 == readed) {
   73: 			SETERR;
   74: 			break;
   75: 		} else {
   76: 			z.avail_in = readed;
   77: 			z.next_in = ibuf;
   78: 		}
   79: 
   80: 		// check flush flag
   81: 		flush = syncEOF(inf) ? Z_FINISH : Z_NO_FLUSH;
   82: 
   83: 		do {
   84: 			// compress and write to file
   85: 			z.avail_out = readed;
   86: 			z.next_out = obuf;
   87: 			ret = deflate(&z, flush);
   88: 			if (Z_STREAM_ERROR == ret) {
   89: 				syncSetErr(ENOEXEC, "Error:: can`t deflate !!!\n");
   90: 				flush = Z_FINISH;
   91: 				ret = -1;
   92: 				break;
   93: 			}
   94: 
   95: 			ret = write(outf, obuf, readed - z.avail_out);
   96: 			if (-1 == ret || ret != readed - z.avail_out) {
   97: 				SETERR;
   98: 				flush = Z_FINISH;
   99: 				ret = -1;
  100: 				break;
  101: 			}
  102: 		} while (!z.avail_out);
  103: 
  104: 		if (z.avail_in) {
  105: 			syncSetErr(ENOEXEC, "Error:: can`t get all input for deflate !!!\n");
  106: 			ret = -1;
  107: 			break;
  108: 		}
  109: 	} while (flush != Z_FINISH);
  110: 
  111: 	// free zlib
  112: 	deflateEnd(&z);
  113: 	return ret;
  114: }
  115: 
  116: int sync_Inflate(int inf, int outf)
  117: {
  118: 	z_stream z;
  119: 	int ret, flush;
  120: 	u_char ibuf[Z_CHUNK], obuf[Z_CHUNK];
  121: 
  122: 	lseek(outf, 0l, SEEK_SET);
  123: 	if (lseek(inf, 0l, SEEK_SET) == -1) {
  124: 		SETERR;
  125: 		return -1;
  126: 	}
  127: 
  128: 	// init zlib
  129: 	memset(&z, 0, sizeof z);
  130: 	ret = inflateInit(&z);
  131: 	if (ret)
  132: 		return ret;
  133: 
  134: 	do {
  135: 		// set input buffer for decompress
  136: 		ret = read(inf, ibuf, Z_CHUNK);
  137: 		if (-1 == ret) {
  138: 			SETERR;
  139: 			break;
  140: 		}
  141: 		if (!ret)
  142: 			break;
  143: 		z.avail_in = ret;
  144: 		z.next_in = ibuf;
  145: 
  146: 		flush = Z_NO_FLUSH;
  147: 
  148: 		do {
  149: 			// decompress and write to file
  150: 			z.avail_out = Z_CHUNK;
  151: 			z.next_out = obuf;
  152: 			ret = inflate(&z, flush);
  153: 			switch (ret) {
  154: 				case Z_NEED_DICT:
  155: 				case Z_DATA_ERROR:
  156: 				case Z_MEM_ERROR:
  157: 					syncSetErr(ENOEXEC, "Error:: can`t inflate !!!\n");
  158: 					flush = Z_STREAM_END;
  159: 					ret = -1;
  160: 					break;
  161: 			}
  162: 
  163: 			ret = write(outf, obuf, Z_CHUNK - z.avail_out);
  164: 			if (-1 == ret || ret != Z_CHUNK - z.avail_out) {
  165: 				SETERR;
  166: 				flush = Z_STREAM_END;
  167: 				ret = -1;
  168: 				break;
  169: 			}
  170: 		} while (!z.avail_out);
  171: 	} while (flush != Z_STREAM_END);
  172: 
  173: 	// free zlib
  174: 	inflateEnd(&z);
  175: 	return ret;
  176: }

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