Annotation of libaitsync/src/zc.c, revision 1.5
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.5 ! misho 6: * $Id: zc.c,v 1.4.6.1 2014/02/04 16:39:00 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.5 ! misho 15: Copyright 2004 - 2014
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.4 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.4 misho 68: LOGERR;
1.1 misho 69: return -1;
70: }
71:
1.4 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.4 misho 79: /* set input buffer for compress */
1.1 misho 80: readed = read(inf, ibuf, Z_CHUNK);
81: if (-1 == readed) {
1.4 misho 82: LOGERR;
1.1 misho 83: break;
84: } else {
85: z.avail_in = readed;
86: z.next_in = ibuf;
87: }
88:
1.4 misho 89: /* check flush flag */
90: flush = sync_EOF(inf) ? Z_FINISH : Z_NO_FLUSH;
1.1 misho 91:
92: do {
1.4 misho 93: /* compress and write to file */
1.1 misho 94: z.avail_out = readed;
95: z.next_out = obuf;
96: ret = deflate(&z, flush);
97: if (Z_STREAM_ERROR == ret) {
1.4 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.4 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.4 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:
1.4 misho 120: /* free zlib */
1.1 misho 121: deflateEnd(&z);
122: return ret;
123: }
124:
1.3 misho 125: /*
1.4 misho 126: * sync_Inflate() - LibZ inflate data
127: *
1.3 misho 128: * @inf = Input file
129: * @outf = Output file
130: * return: -1 error, != -1 ok
131: */
132: int
133: sync_Inflate(int inf, int outf)
1.1 misho 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) {
1.4 misho 141: LOGERR;
1.1 misho 142: return -1;
143: }
144:
1.4 misho 145: /* init zlib */
1.1 misho 146: memset(&z, 0, sizeof z);
147: ret = inflateInit(&z);
148: if (ret)
149: return ret;
150:
151: do {
1.4 misho 152: /* set input buffer for decompress */
1.1 misho 153: ret = read(inf, ibuf, Z_CHUNK);
154: if (-1 == ret) {
1.4 misho 155: LOGERR;
1.1 misho 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 {
1.4 misho 166: /* decompress and write to file */
1.1 misho 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:
1.4 misho 174: sync_SetErr(ENOEXEC, "Error:: can`t inflate !!!\n");
1.1 misho 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) {
1.4 misho 182: LOGERR;
1.1 misho 183: flush = Z_STREAM_END;
184: ret = -1;
185: break;
186: }
187: } while (!z.avail_out);
188: } while (flush != Z_STREAM_END);
189:
1.4 misho 190: /* free zlib */
1.1 misho 191: inflateEnd(&z);
192: return ret;
193: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>