1: #include "global.h"
2: #include "file.h"
3: #include "zc.h"
4:
5:
6: int sync_Deflate(int inf, int outf, int level)
7: {
8: z_stream z;
9: int ret, flush, readed;
10: u_char ibuf[Z_CHUNK], obuf[Z_CHUNK];
11:
12: lseek(outf, 0l, SEEK_SET);
13: if (lseek(inf, 0l, SEEK_SET) == -1) {
14: SETERR;
15: return -1;
16: }
17:
18: // init zlib
19: memset(&z, 0, sizeof z);
20: ret = deflateInit(&z, level);
21: if (ret)
22: return ret;
23:
24: do {
25: // set input buffer for compress
26: readed = read(inf, ibuf, Z_CHUNK);
27: if (-1 == readed) {
28: SETERR;
29: break;
30: } else {
31: z.avail_in = readed;
32: z.next_in = ibuf;
33: }
34:
35: // check flush flag
36: flush = syncEOF(inf) ? Z_FINISH : Z_NO_FLUSH;
37:
38: do {
39: // compress and write to file
40: z.avail_out = readed;
41: z.next_out = obuf;
42: ret = deflate(&z, flush);
43: if (Z_STREAM_ERROR == ret) {
44: syncSetErr(ENOEXEC, "Error:: can`t deflate !!!\n");
45: flush = Z_FINISH;
46: ret = -1;
47: break;
48: }
49:
50: ret = write(outf, obuf, readed - z.avail_out);
51: if (-1 == ret || ret != readed - z.avail_out) {
52: SETERR;
53: flush = Z_FINISH;
54: ret = -1;
55: break;
56: }
57: } while (!z.avail_out);
58:
59: if (z.avail_in) {
60: syncSetErr(ENOEXEC, "Error:: can`t get all input for deflate !!!\n");
61: ret = -1;
62: break;
63: }
64: } while (flush != Z_FINISH);
65:
66: // free zlib
67: deflateEnd(&z);
68: return ret;
69: }
70:
71: int sync_Inflate(int inf, int outf)
72: {
73: z_stream z;
74: int ret, flush;
75: u_char ibuf[Z_CHUNK], obuf[Z_CHUNK];
76:
77: lseek(outf, 0l, SEEK_SET);
78: if (lseek(inf, 0l, SEEK_SET) == -1) {
79: SETERR;
80: return -1;
81: }
82:
83: // init zlib
84: memset(&z, 0, sizeof z);
85: ret = inflateInit(&z);
86: if (ret)
87: return ret;
88:
89: do {
90: // set input buffer for decompress
91: ret = read(inf, ibuf, Z_CHUNK);
92: if (-1 == ret) {
93: SETERR;
94: break;
95: }
96: if (!ret)
97: break;
98: z.avail_in = ret;
99: z.next_in = ibuf;
100:
101: flush = Z_NO_FLUSH;
102:
103: do {
104: // decompress and write to file
105: z.avail_out = Z_CHUNK;
106: z.next_out = obuf;
107: ret = inflate(&z, flush);
108: switch (ret) {
109: case Z_NEED_DICT:
110: case Z_DATA_ERROR:
111: case Z_MEM_ERROR:
112: syncSetErr(ENOEXEC, "Error:: can`t inflate !!!\n");
113: flush = Z_STREAM_END;
114: ret = -1;
115: break;
116: }
117:
118: ret = write(outf, obuf, Z_CHUNK - z.avail_out);
119: if (-1 == ret || ret != Z_CHUNK - z.avail_out) {
120: SETERR;
121: flush = Z_STREAM_END;
122: ret = -1;
123: break;
124: }
125: } while (!z.avail_out);
126: } while (flush != Z_STREAM_END);
127:
128: // free zlib
129: inflateEnd(&z);
130: return ret;
131: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>