2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 extern int do_compression;
26 /* non-compressing recv token */
27 static int simple_recv_token(int f,char **data)
29 static int residue = 0;
30 static char *buf = NULL;
34 buf = (char *)malloc(CHUNK_SIZE);
35 if (!buf) out_of_memory("simple_recv_token");
46 n = MIN(CHUNK_SIZE,residue);
53 /* non-compressing send token */
54 static void simple_send_token(int f,int token,
55 struct map_struct *buf,int offset,int n)
60 int n1 = MIN(CHUNK_SIZE,n-l);
62 write_buf(f,map_ptr(buf,offset+l,n1),n1);
66 write_int(f,-(token+1));
70 /* Memory allocation/freeing routines, called by zlib stuff. */
72 z_alloc(void *opaque, uInt items, uInt size)
74 return malloc(items * size);
78 z_free(void *opaque, void *adrs, uInt nbytes)
83 /* Flag bytes in compressed stream are encoded as follows: */
84 #define END_FLAG 0 /* that's all folks */
85 #define TOKEN_LONG 0x20 /* followed by 32-bit token number */
86 #define TOKENRUN_LONG 0x21 /* ditto with 16-bit run count */
87 #define DEFLATED_DATA 0x40 /* + 6-bit high len, then low len byte */
88 #define TOKEN_REL 0x80 /* + 6-bit relative token number */
89 #define TOKENRUN_REL 0xc0 /* ditto with 16-bit run count */
91 #define MAX_DATA_COUNT 16383 /* fit 14 bit count into 2 bytes with flags */
93 /* For coding runs of tokens */
94 static int last_token = -1;
96 static int last_run_end;
99 static z_stream tx_strm;
102 static char *obuf = NULL;
104 /* Send a deflated token */
106 send_deflated_token(int f, int token,
107 struct map_struct *buf, int offset, int nb, int toklen)
110 static int init_done;
112 if (last_token == -1) {
115 tx_strm.next_in = NULL;
116 tx_strm.zalloc = z_alloc;
117 tx_strm.zfree = z_free;
118 if (deflateInit2(&tx_strm, Z_DEFAULT_COMPRESSION, 8,
119 -15, 8, Z_DEFAULT_STRATEGY, -4) != Z_OK) {
120 fprintf(FERROR, "compression init failed\n");
123 if ((obuf = malloc(MAX_DATA_COUNT+2)) == NULL)
124 out_of_memory("send_deflated_token");
127 deflateReset(&tx_strm);
131 } else if (nb != 0 || token != last_token + 1
132 || token >= run_start + 65536) {
133 /* output previous run */
134 r = run_start - last_run_end;
135 n = last_token - run_start;
136 if (r >= 0 && r <= 63) {
137 write_byte(f, (n==0? TOKEN_REL: TOKENRUN_REL) + r);
139 write_byte(f, (n==0? TOKEN_LONG: TOKENRUN_LONG));
140 write_int(f, run_start);
144 write_byte(f, n >> 8);
146 last_run_end = last_token;
153 /* deflate the data starting at offset */
154 tx_strm.avail_in = 0;
155 tx_strm.avail_out = 0;
157 if (tx_strm.avail_in == 0 && nb != 0) {
158 /* give it some more input */
159 n = MIN(nb, CHUNK_SIZE);
160 tx_strm.next_in = map_ptr(buf, offset, n);
161 tx_strm.avail_in = n;
165 if (tx_strm.avail_out == 0) {
166 tx_strm.next_out = obuf + 2;
167 tx_strm.avail_out = MAX_DATA_COUNT;
169 r = deflate(&tx_strm, nb? Z_NO_FLUSH: Z_PACKET_FLUSH);
171 fprintf(FERROR, "deflate returned %d\n", r);
174 if (nb == 0 || tx_strm.avail_out == 0) {
175 n = MAX_DATA_COUNT - tx_strm.avail_out;
177 obuf[0] = DEFLATED_DATA + (n >> 8);
179 write_buf(f, obuf, n+2);
182 } while (nb != 0 || tx_strm.avail_out == 0);
186 /* add the data in the current block to the compressor's
187 history and hash table */
188 tx_strm.next_in = map_ptr(buf, offset, toklen);
189 tx_strm.avail_in = toklen;
190 tx_strm.next_out = NULL;
191 tx_strm.avail_out = 2 * toklen;
192 r = deflate(&tx_strm, Z_INSERT_ONLY);
193 if (r != Z_OK || tx_strm.avail_in != 0) {
194 fprintf(FERROR, "deflate on token returned %d (%d bytes left)\n",
195 r, tx_strm.avail_in);
200 /* end of file - clean up */
201 write_byte(f, END_FLAG);
206 /* tells us what the receiver is in the middle of doing */
207 static enum { r_init, r_idle, r_running, r_inflating, r_inflated } recv_state;
209 /* for inflating stuff */
210 static z_stream rx_strm;
214 /* for decoding runs of tokens */
218 /* Receive a deflated token and inflate it */
220 recv_deflated_token(int f, char **data)
223 static int init_done = 0;
226 switch (recv_state) {
229 rx_strm.next_out = NULL;
230 rx_strm.zalloc = z_alloc;
231 rx_strm.zfree = z_free;
232 if (inflateInit2(&rx_strm, -15) != Z_OK) {
233 fprintf(FERROR, "inflate init failed\n");
236 if ((cbuf = malloc(MAX_DATA_COUNT)) == NULL
237 || (dbuf = malloc(CHUNK_SIZE)) == NULL)
238 out_of_memory("recv_deflated_token");
241 inflateReset(&rx_strm);
249 if ((flag & 0xC0) == DEFLATED_DATA) {
250 n = ((flag & 0x3f) << 8) + read_byte(f);
251 read_buf(f, cbuf, n);
252 rx_strm.next_in = cbuf;
253 rx_strm.avail_in = n;
254 recv_state = r_inflating;
257 if (recv_state == r_inflated) {
258 /* check previous inflated stuff ended correctly */
259 rx_strm.avail_in = 0;
260 rx_strm.next_out = dbuf;
261 rx_strm.avail_out = CHUNK_SIZE;
262 r = inflate(&rx_strm, Z_PACKET_FLUSH);
263 n = CHUNK_SIZE - rx_strm.avail_out;
264 if (r != Z_OK || n != 0) {
265 fprintf(FERROR, "inflate flush returned %d (%d bytes)\n",
271 if (flag == END_FLAG) {
272 /* that's all folks */
277 /* here we have a token of some kind */
278 if (flag & TOKEN_REL) {
279 rx_token += flag & 0x3f;
282 rx_token = read_int(f);
284 rx_run = read_byte(f);
285 rx_run += read_byte(f) << 8;
286 recv_state = r_running;
288 return -1 - rx_token;
291 rx_strm.next_out = dbuf;
292 rx_strm.avail_out = CHUNK_SIZE;
293 r = inflate(&rx_strm, Z_NO_FLUSH);
294 n = CHUNK_SIZE - rx_strm.avail_out;
295 if (r != Z_OK || n == 0) {
296 fprintf(FERROR, "inflate returned %d (%d bytes)\n", r, n);
299 if (rx_strm.avail_out != 0)
300 recv_state = r_inflated;
308 return -1 - rx_token;
314 * put the data corresponding to a token that we've just returned
315 * from recv_deflated_token into the decompressor's history buffer.
318 see_deflate_token(char *buf, int len)
322 rx_strm.next_in = buf;
323 rx_strm.avail_in = len;
324 r = inflateIncomp(&rx_strm);
326 fprintf(FERROR, "inflateIncomp returned %d\n", r);
332 * transmit a verbatim buffer of length n followed by a token
333 * If token == -1 then we have reached EOF
334 * If n == 0 then don't send a buffer
336 void send_token(int f,int token,struct map_struct *buf,int offset,
339 if (!do_compression) {
340 simple_send_token(f,token,buf,offset,n);
342 send_deflated_token(f, token, buf, offset, n, toklen);
348 * receive a token or buffer from the other end. If the reurn value is >0 then
349 * it is a data buffer of that length, and *data will point at the data.
350 * if the return value is -i then it represents token i-1
351 * if the return value is 0 then the end has been reached
353 int recv_token(int f,char **data)
357 if (!do_compression) {
358 tok = simple_recv_token(f,data);
360 tok = recv_deflated_token(f, data);
366 * look at the data corresponding to a token, if necessary
368 void see_token(char *data, int toklen)
373 see_deflate_token(data, toklen);