File:  [ELWIX - Embedded LightWeight unIX -] / libaitsync / src / zc.c
Revision 1.5: download - view: text, annotated - select for diffs - revision graph
Tue Feb 4 16:58:17 2014 UTC (10 years, 4 months ago) by misho
Branches: MAIN
CVS tags: sync2_3, sync2_2, SYNC2_2, SYNC2_1, HEAD
version 2.1

    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.5 2014/02/04 16:58:17 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 - 2014
   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: /*
   52:  * sync_Deflate() - LibZ deflate data
   53:  *
   54:  * @inf = Input file
   55:  * @outf = Output file
   56:  * @level = Compress level
   57:  * return: -1 error, != -1 ok
   58: */
   59: int
   60: sync_Deflate(int inf, int outf, int level)
   61: {
   62: 	z_stream z;
   63: 	int ret, flush, readed;
   64: 	u_char ibuf[Z_CHUNK], obuf[Z_CHUNK];
   65: 
   66: 	lseek(outf, 0l, SEEK_SET);
   67: 	if (lseek(inf, 0l, SEEK_SET) == -1) {
   68: 		LOGERR;
   69: 		return -1;
   70: 	}
   71: 
   72: 	/* init zlib */
   73: 	memset(&z, 0, sizeof z);
   74: 	ret = deflateInit(&z, level);
   75: 	if (ret)
   76: 		return ret;
   77: 
   78: 	do {
   79: 		/* set input buffer for compress */
   80: 		readed = read(inf, ibuf, Z_CHUNK);
   81: 		if (-1 == readed) {
   82: 			LOGERR;
   83: 			break;
   84: 		} else {
   85: 			z.avail_in = readed;
   86: 			z.next_in = ibuf;
   87: 		}
   88: 
   89: 		/* check flush flag */
   90: 		flush = sync_EOF(inf) ? Z_FINISH : Z_NO_FLUSH;
   91: 
   92: 		do {
   93: 			/* compress and write to file */
   94: 			z.avail_out = readed;
   95: 			z.next_out = obuf;
   96: 			ret = deflate(&z, flush);
   97: 			if (Z_STREAM_ERROR == ret) {
   98: 				sync_SetErr(ENOEXEC, "Error:: can`t deflate !!!\n");
   99: 				flush = Z_FINISH;
  100: 				ret = -1;
  101: 				break;
  102: 			}
  103: 
  104: 			ret = write(outf, obuf, readed - z.avail_out);
  105: 			if (-1 == ret || ret != readed - z.avail_out) {
  106: 				LOGERR;
  107: 				flush = Z_FINISH;
  108: 				ret = -1;
  109: 				break;
  110: 			}
  111: 		} while (!z.avail_out);
  112: 
  113: 		if (z.avail_in) {
  114: 			sync_SetErr(ENOEXEC, "Error:: can`t get all input for deflate !!!\n");
  115: 			ret = -1;
  116: 			break;
  117: 		}
  118: 	} while (flush != Z_FINISH);
  119: 
  120: 	/* free zlib */
  121: 	deflateEnd(&z);
  122: 	return ret;
  123: }
  124: 
  125: /*
  126:  * sync_Inflate() - LibZ inflate data
  127:  *
  128:  * @inf = Input file
  129:  * @outf = Output file
  130:  * return: -1 error, != -1 ok
  131: */
  132: int
  133: sync_Inflate(int inf, int outf)
  134: {
  135: 	z_stream z;
  136: 	int ret, flush;
  137: 	u_char ibuf[Z_CHUNK], obuf[Z_CHUNK];
  138: 
  139: 	lseek(outf, 0l, SEEK_SET);
  140: 	if (lseek(inf, 0l, SEEK_SET) == -1) {
  141: 		LOGERR;
  142: 		return -1;
  143: 	}
  144: 
  145: 	/* init zlib */
  146: 	memset(&z, 0, sizeof z);
  147: 	ret = inflateInit(&z);
  148: 	if (ret)
  149: 		return ret;
  150: 
  151: 	do {
  152: 		/* set input buffer for decompress */
  153: 		ret = read(inf, ibuf, Z_CHUNK);
  154: 		if (-1 == ret) {
  155: 			LOGERR;
  156: 			break;
  157: 		}
  158: 		if (!ret)
  159: 			break;
  160: 		z.avail_in = ret;
  161: 		z.next_in = ibuf;
  162: 
  163: 		flush = Z_NO_FLUSH;
  164: 
  165: 		do {
  166: 			/* decompress and write to file */
  167: 			z.avail_out = Z_CHUNK;
  168: 			z.next_out = obuf;
  169: 			ret = inflate(&z, flush);
  170: 			switch (ret) {
  171: 				case Z_NEED_DICT:
  172: 				case Z_DATA_ERROR:
  173: 				case Z_MEM_ERROR:
  174: 					sync_SetErr(ENOEXEC, "Error:: can`t inflate !!!\n");
  175: 					flush = Z_STREAM_END;
  176: 					ret = -1;
  177: 					break;
  178: 			}
  179: 
  180: 			ret = write(outf, obuf, Z_CHUNK - z.avail_out);
  181: 			if (-1 == ret || ret != Z_CHUNK - z.avail_out) {
  182: 				LOGERR;
  183: 				flush = Z_STREAM_END;
  184: 				ret = -1;
  185: 				break;
  186: 			}
  187: 		} while (!z.avail_out);
  188: 	} while (flush != Z_STREAM_END);
  189: 
  190: 	/* free zlib */
  191: 	inflateEnd(&z);
  192: 	return ret;
  193: }

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