Annotation of libaitsync/src/zc.c, revision 1.3.2.1
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.2.1 ! misho 6: * $Id: zc.c,v 1.3 2012/07/22 22:09:47 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: /*
1.3.2.1 ! misho 52: * sync_Deflate() - LibZ deflate data
! 53: *
1.3 misho 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)
1.1 misho 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) {
1.3.2.1 ! misho 68: LOGERR;
1.1 misho 69: return -1;
70: }
71:
1.3.2.1 ! misho 72: /* init zlib */
1.1 misho 73: memset(&z, 0, sizeof z);
74: ret = deflateInit(&z, level);
75: if (ret)
76: return ret;
77:
78: do {
1.3.2.1 ! misho 79: /* set input buffer for compress */
1.1 misho 80: readed = read(inf, ibuf, Z_CHUNK);
81: if (-1 == readed) {
1.3.2.1 ! misho 82: LOGERR;
1.1 misho 83: break;
84: } else {
85: z.avail_in = readed;
86: z.next_in = ibuf;
87: }
88:
1.3.2.1 ! misho 89: /* check flush flag */
! 90: flush = sync_EOF(inf) ? Z_FINISH : Z_NO_FLUSH;
1.1 misho 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) {
1.3.2.1 ! misho 98: sync_SetErr(ENOEXEC, "Error:: can`t deflate !!!\n");
1.1 misho 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) {
1.3.2.1 ! misho 106: LOGERR;
1.1 misho 107: flush = Z_FINISH;
108: ret = -1;
109: break;
110: }
111: } while (!z.avail_out);
112:
113: if (z.avail_in) {
1.3.2.1 ! misho 114: sync_SetErr(ENOEXEC, "Error:: can`t get all input for deflate !!!\n");
1.1 misho 115: ret = -1;
116: break;
117: }
118: } while (flush != Z_FINISH);
119:
120: // free zlib
121: deflateEnd(&z);
122: return ret;
123: }
124:
1.3 misho 125: /*
126: * sync_Inflate() LibZ inflate data
127: * @inf = Input file
128: * @outf = Output file
129: * return: -1 error, != -1 ok
130: */
131: int
132: sync_Inflate(int inf, int outf)
1.1 misho 133: {
134: z_stream z;
135: int ret, flush;
136: u_char ibuf[Z_CHUNK], obuf[Z_CHUNK];
137:
138: lseek(outf, 0l, SEEK_SET);
139: if (lseek(inf, 0l, SEEK_SET) == -1) {
1.3.2.1 ! misho 140: LOGERR;
1.1 misho 141: return -1;
142: }
143:
144: // init zlib
145: memset(&z, 0, sizeof z);
146: ret = inflateInit(&z);
147: if (ret)
148: return ret;
149:
150: do {
151: // set input buffer for decompress
152: ret = read(inf, ibuf, Z_CHUNK);
153: if (-1 == ret) {
1.3.2.1 ! misho 154: LOGERR;
1.1 misho 155: break;
156: }
157: if (!ret)
158: break;
159: z.avail_in = ret;
160: z.next_in = ibuf;
161:
162: flush = Z_NO_FLUSH;
163:
164: do {
165: // decompress and write to file
166: z.avail_out = Z_CHUNK;
167: z.next_out = obuf;
168: ret = inflate(&z, flush);
169: switch (ret) {
170: case Z_NEED_DICT:
171: case Z_DATA_ERROR:
172: case Z_MEM_ERROR:
1.3.2.1 ! misho 173: sync_SetErr(ENOEXEC, "Error:: can`t inflate !!!\n");
1.1 misho 174: flush = Z_STREAM_END;
175: ret = -1;
176: break;
177: }
178:
179: ret = write(outf, obuf, Z_CHUNK - z.avail_out);
180: if (-1 == ret || ret != Z_CHUNK - z.avail_out) {
1.3.2.1 ! misho 181: LOGERR;
1.1 misho 182: flush = Z_STREAM_END;
183: ret = -1;
184: break;
185: }
186: } while (!z.avail_out);
187: } while (flush != Z_STREAM_END);
188:
189: // free zlib
190: inflateEnd(&z);
191: return ret;
192: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>