blob: 67acbd988f9979c3c3b3a9cb96c1abfa67987e5d [file] [log] [blame]
Wayne Davisond67c8bd2004-06-18 16:22:14 +00001/*
Andrew Tridgell70d794d1996-07-01 05:55:05 +00002 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
Wayne Davisond67c8bd2004-06-18 16:22:14 +00004
Andrew Tridgell70d794d1996-07-01 05:55:05 +00005 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.
Wayne Davisond67c8bd2004-06-18 16:22:14 +00009
Andrew Tridgell70d794d1996-07-01 05:55:05 +000010 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.
Wayne Davisond67c8bd2004-06-18 16:22:14 +000014
Andrew Tridgell70d794d1996-07-01 05:55:05 +000015 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.
18*/
19
20#include "rsync.h"
Paul Mackerras5914bf11998-05-22 06:58:52 +000021#include "zlib/zlib.h"
Andrew Tridgell70d794d1996-07-01 05:55:05 +000022
23extern int do_compression;
Wayne Davisond67c8bd2004-06-18 16:22:14 +000024extern int module_id;
Wayne Davisond67c8bd2004-06-18 16:22:14 +000025
Andrew Tridgell83fff1a1998-11-20 22:26:29 +000026static int compression_level = Z_DEFAULT_COMPRESSION;
Andrew Tridgell70d794d1996-07-01 05:55:05 +000027
Andrew Tridgell83fff1a1998-11-20 22:26:29 +000028/* determine the compression level based on a wildcard filename list */
29void set_compression(char *fname)
30{
Andrew Tridgell83fff1a1998-11-20 22:26:29 +000031 char *dont;
32 char *tok;
33
Wayne Davisond67c8bd2004-06-18 16:22:14 +000034 if (!do_compression)
35 return;
Andrew Tridgell83fff1a1998-11-20 22:26:29 +000036
David Dykstra63f07741998-11-25 15:37:50 +000037 compression_level = Z_DEFAULT_COMPRESSION;
Andrew Tridgell83fff1a1998-11-20 22:26:29 +000038 dont = lp_dont_compress(module_id);
39
Wayne Davisond67c8bd2004-06-18 16:22:14 +000040 if (!dont || !*dont)
41 return;
Andrew Tridgell83fff1a1998-11-20 22:26:29 +000042
Wayne Davisond67c8bd2004-06-18 16:22:14 +000043 if (dont[0] == '*' && !dont[1]) {
David Dykstra63f07741998-11-25 15:37:50 +000044 /* an optimization to skip the rest of this routine */
45 compression_level = 0;
46 return;
47 }
48
Andrew Tridgell83fff1a1998-11-20 22:26:29 +000049 dont = strdup(dont);
50 fname = strdup(fname);
Wayne Davisond67c8bd2004-06-18 16:22:14 +000051 if (!dont || !fname)
52 return;
Andrew Tridgell83fff1a1998-11-20 22:26:29 +000053
54 strlower(dont);
55 strlower(fname);
56
Wayne Davisond67c8bd2004-06-18 16:22:14 +000057 for (tok = strtok(dont, " "); tok; tok = strtok(NULL, " ")) {
Wayne Davisonfe332032003-07-30 06:12:27 +000058 if (wildmatch(tok, fname)) {
Andrew Tridgell83fff1a1998-11-20 22:26:29 +000059 compression_level = 0;
60 break;
61 }
62 }
63 free(dont);
64 free(fname);
65}
Andrew Tridgell70d794d1996-07-01 05:55:05 +000066
67/* non-compressing recv token */
Wayne Davison7fcbf9e2005-01-01 21:08:20 +000068static int32 simple_recv_token(int f, char **data)
Andrew Tridgell70d794d1996-07-01 05:55:05 +000069{
Wayne Davisonacc461c2005-02-14 08:19:32 +000070 static int32 residue;
Andrew Tridgellc5eb3651998-05-21 05:57:15 +000071 static char *buf;
Wayne Davison7fcbf9e2005-01-01 21:08:20 +000072 int32 n;
Andrew Tridgell70d794d1996-07-01 05:55:05 +000073
Andrew Tridgellc5eb3651998-05-21 05:57:15 +000074 if (!buf) {
Wayne Davison58cadc82003-12-06 21:07:27 +000075 buf = new_array(char, CHUNK_SIZE);
Wayne Davisond67c8bd2004-06-18 16:22:14 +000076 if (!buf)
77 out_of_memory("simple_recv_token");
Andrew Tridgellc5eb3651998-05-21 05:57:15 +000078 }
Andrew Tridgell70d794d1996-07-01 05:55:05 +000079
Andrew Tridgellc5eb3651998-05-21 05:57:15 +000080 if (residue == 0) {
Wayne Davison7fcbf9e2005-01-01 21:08:20 +000081 int32 i = read_int(f);
Wayne Davisond67c8bd2004-06-18 16:22:14 +000082 if (i <= 0)
83 return i;
Andrew Tridgellc5eb3651998-05-21 05:57:15 +000084 residue = i;
85 }
Andrew Tridgell70d794d1996-07-01 05:55:05 +000086
Andrew Tridgellc5eb3651998-05-21 05:57:15 +000087 *data = buf;
88 n = MIN(CHUNK_SIZE,residue);
89 residue -= n;
90 read_buf(f,buf,n);
91 return n;
Andrew Tridgell70d794d1996-07-01 05:55:05 +000092}
93
94
95/* non-compressing send token */
Wayne Davisonacc461c2005-02-14 08:19:32 +000096static void simple_send_token(int f, int32 token, struct map_struct *buf,
Wayne Davison7fcbf9e2005-01-01 21:08:20 +000097 OFF_T offset, int32 n)
Andrew Tridgell70d794d1996-07-01 05:55:05 +000098{
Andrew Tridgellc5eb3651998-05-21 05:57:15 +000099 if (n > 0) {
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000100 int32 len = 0;
101 while (len < n) {
102 int32 n1 = MIN(CHUNK_SIZE, n-len);
103 write_int(f, n1);
104 write_buf(f, map_ptr(buf, offset+len, n1), n1);
105 len += n1;
Andrew Tridgellc5eb3651998-05-21 05:57:15 +0000106 }
107 }
Andrew Tridgell45f133b1998-05-22 01:53:02 +0000108 /* a -2 token means to send data only and no token */
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000109 if (token != -2)
110 write_int(f, -(token+1));
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000111}
112
113
Paul Mackerras861c20b1996-07-03 04:05:46 +0000114/* Flag bytes in compressed stream are encoded as follows: */
115#define END_FLAG 0 /* that's all folks */
116#define TOKEN_LONG 0x20 /* followed by 32-bit token number */
117#define TOKENRUN_LONG 0x21 /* ditto with 16-bit run count */
118#define DEFLATED_DATA 0x40 /* + 6-bit high len, then low len byte */
119#define TOKEN_REL 0x80 /* + 6-bit relative token number */
120#define TOKENRUN_REL 0xc0 /* ditto with 16-bit run count */
121
122#define MAX_DATA_COUNT 16383 /* fit 14 bit count into 2 bytes with flags */
123
Wayne Davison24733912003-07-04 07:47:09 +0000124/* zlib.h says that if we want to be able to compress something in a single
125 * call, avail_out must be at least 0.1% larger than avail_in plus 12 bytes.
126 * We'll add in 0.1%+16, just to be safe (and we'll avoid floating point,
127 * to ensure that this is a compile-time value). */
128#define AVAIL_OUT_SIZE(avail_in_size) ((avail_in_size)*1001/1000+16)
129
Paul Mackerras861c20b1996-07-03 04:05:46 +0000130/* For coding runs of tokens */
Wayne Davisonacc461c2005-02-14 08:19:32 +0000131static int32 last_token = -1;
132static int32 run_start;
133static int32 last_run_end;
Paul Mackerras861c20b1996-07-03 04:05:46 +0000134
135/* Deflation state */
136static z_stream tx_strm;
137
138/* Output buffer */
Andrew Tridgell3a6a3661998-03-23 12:52:57 +0000139static char *obuf;
Wayne Davison24733912003-07-04 07:47:09 +0000140
141/* We want obuf to be able to hold both MAX_DATA_COUNT+2 bytes as well as
142 * AVAIL_OUT_SIZE(CHUNK_SIZE) bytes, so make sure that it's large enough. */
143#if MAX_DATA_COUNT+2 > AVAIL_OUT_SIZE(CHUNK_SIZE)
144#define OBUF_SIZE (MAX_DATA_COUNT+2)
145#else
146#define OBUF_SIZE AVAIL_OUT_SIZE(CHUNK_SIZE)
147#endif
Paul Mackerras861c20b1996-07-03 04:05:46 +0000148
149/* Send a deflated token */
150static void
Wayne Davisonacc461c2005-02-14 08:19:32 +0000151send_deflated_token(int f, int32 token, struct map_struct *buf, OFF_T offset,
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000152 int32 nb, int32 toklen)
Paul Mackerras861c20b1996-07-03 04:05:46 +0000153{
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000154 int32 n, r;
Paul Mackerras5914bf11998-05-22 06:58:52 +0000155 static int init_done, flush_pending;
Paul Mackerras861c20b1996-07-03 04:05:46 +0000156
Paul Mackerras5914bf11998-05-22 06:58:52 +0000157 if (last_token == -1) {
158 /* initialization */
159 if (!init_done) {
160 tx_strm.next_in = NULL;
161 tx_strm.zalloc = NULL;
162 tx_strm.zfree = NULL;
Andrew Tridgell83fff1a1998-11-20 22:26:29 +0000163 if (deflateInit2(&tx_strm, compression_level,
Paul Mackerras5914bf11998-05-22 06:58:52 +0000164 Z_DEFLATED, -15, 8,
165 Z_DEFAULT_STRATEGY) != Z_OK) {
166 rprintf(FERROR, "compression init failed\n");
Andrew Tridgell65417571998-11-03 07:08:27 +0000167 exit_cleanup(RERR_STREAMIO);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000168 }
Wayne Davison58cadc82003-12-06 21:07:27 +0000169 if ((obuf = new_array(char, OBUF_SIZE)) == NULL)
Paul Mackerras5914bf11998-05-22 06:58:52 +0000170 out_of_memory("send_deflated_token");
171 init_done = 1;
172 } else
173 deflateReset(&tx_strm);
174 last_run_end = 0;
175 run_start = token;
176 flush_pending = 0;
Paul Mackerras861c20b1996-07-03 04:05:46 +0000177
Paul Mackerras5914bf11998-05-22 06:58:52 +0000178 } else if (last_token == -2) {
179 run_start = token;
Paul Mackerras861c20b1996-07-03 04:05:46 +0000180
Paul Mackerras5914bf11998-05-22 06:58:52 +0000181 } else if (nb != 0 || token != last_token + 1
182 || token >= run_start + 65536) {
183 /* output previous run */
184 r = run_start - last_run_end;
185 n = last_token - run_start;
186 if (r >= 0 && r <= 63) {
187 write_byte(f, (n==0? TOKEN_REL: TOKENRUN_REL) + r);
188 } else {
189 write_byte(f, (n==0? TOKEN_LONG: TOKENRUN_LONG));
190 write_int(f, run_start);
Paul Mackerras861c20b1996-07-03 04:05:46 +0000191 }
Paul Mackerras5914bf11998-05-22 06:58:52 +0000192 if (n != 0) {
193 write_byte(f, n);
194 write_byte(f, n >> 8);
195 }
196 last_run_end = last_token;
197 run_start = token;
Paul Mackerras861c20b1996-07-03 04:05:46 +0000198 }
199
Paul Mackerras5914bf11998-05-22 06:58:52 +0000200 last_token = token;
201
202 if (nb != 0 || flush_pending) {
203 /* deflate the data starting at offset */
204 int flush = Z_NO_FLUSH;
205 tx_strm.avail_in = 0;
206 tx_strm.avail_out = 0;
207 do {
208 if (tx_strm.avail_in == 0 && nb != 0) {
209 /* give it some more input */
210 n = MIN(nb, CHUNK_SIZE);
211 tx_strm.next_in = (Bytef *)
212 map_ptr(buf, offset, n);
213 tx_strm.avail_in = n;
214 nb -= n;
215 offset += n;
216 }
217 if (tx_strm.avail_out == 0) {
218 tx_strm.next_out = (Bytef *)(obuf + 2);
219 tx_strm.avail_out = MAX_DATA_COUNT;
220 if (flush != Z_NO_FLUSH) {
221 /*
222 * We left the last 4 bytes in the
223 * buffer, in case they are the
224 * last 4. Move them to the front.
225 */
226 memcpy(tx_strm.next_out,
227 obuf+MAX_DATA_COUNT-2, 4);
228 tx_strm.next_out += 4;
229 tx_strm.avail_out -= 4;
230 }
231 }
232 if (nb == 0 && token != -2)
233 flush = Z_SYNC_FLUSH;
234 r = deflate(&tx_strm, flush);
235 if (r != Z_OK) {
236 rprintf(FERROR, "deflate returned %d\n", r);
Andrew Tridgell65417571998-11-03 07:08:27 +0000237 exit_cleanup(RERR_STREAMIO);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000238 }
239 if (nb == 0 || tx_strm.avail_out == 0) {
240 n = MAX_DATA_COUNT - tx_strm.avail_out;
241 if (flush != Z_NO_FLUSH) {
242 /*
243 * We have to trim off the last 4
244 * bytes of output when flushing
245 * (they are just 0, 0, ff, ff).
246 */
247 n -= 4;
248 }
249 if (n > 0) {
250 obuf[0] = DEFLATED_DATA + (n >> 8);
251 obuf[1] = n;
252 write_buf(f, obuf, n+2);
253 }
254 }
255 } while (nb != 0 || tx_strm.avail_out == 0);
256 flush_pending = token == -2;
257 }
258
259 if (token == -1) {
260 /* end of file - clean up */
261 write_byte(f, END_FLAG);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000262 } else if (token != -2) {
Wayne Davisonacc461c2005-02-14 08:19:32 +0000263 /* Add the data in the current block to the compressor's
264 * history and hash table. */
Wayne Davison01f439e2005-02-14 08:28:00 +0000265 do {
266 /* Break up long sections in the same way that
267 * see_deflate_token() does. */
268 int32 n1 = toklen > 0xffff ? 0xffff : toklen;
269 toklen -= n1;
270 tx_strm.next_in = (Bytef *)map_ptr(buf, offset, n1);
271 tx_strm.avail_in = n1;
272 tx_strm.next_out = (Bytef *) obuf;
273 tx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
274 r = deflate(&tx_strm, Z_INSERT_ONLY);
275 if (r != Z_OK || tx_strm.avail_in != 0) {
276 rprintf(FERROR, "deflate on token returned %d (%d bytes left)\n",
277 r, tx_strm.avail_in);
278 exit_cleanup(RERR_STREAMIO);
279 }
280 } while (toklen > 0);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000281 }
Paul Mackerras861c20b1996-07-03 04:05:46 +0000282}
283
284
285/* tells us what the receiver is in the middle of doing */
286static enum { r_init, r_idle, r_running, r_inflating, r_inflated } recv_state;
287
288/* for inflating stuff */
289static z_stream rx_strm;
290static char *cbuf;
291static char *dbuf;
292
293/* for decoding runs of tokens */
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000294static int32 rx_token;
295static int32 rx_run;
Paul Mackerras861c20b1996-07-03 04:05:46 +0000296
297/* Receive a deflated token and inflate it */
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000298static int32 recv_deflated_token(int f, char **data)
Paul Mackerras861c20b1996-07-03 04:05:46 +0000299{
Paul Mackerras5914bf11998-05-22 06:58:52 +0000300 static int init_done;
Wayne Davisonacc461c2005-02-14 08:19:32 +0000301 static int32 saved_flag;
302 int32 n, flag;
303 int r;
Paul Mackerras861c20b1996-07-03 04:05:46 +0000304
Paul Mackerras5914bf11998-05-22 06:58:52 +0000305 for (;;) {
306 switch (recv_state) {
307 case r_init:
308 if (!init_done) {
309 rx_strm.next_out = NULL;
310 rx_strm.zalloc = NULL;
311 rx_strm.zfree = NULL;
312 if (inflateInit2(&rx_strm, -15) != Z_OK) {
313 rprintf(FERROR, "inflate init failed\n");
Andrew Tridgell65417571998-11-03 07:08:27 +0000314 exit_cleanup(RERR_STREAMIO);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000315 }
Wayne Davison58cadc82003-12-06 21:07:27 +0000316 if (!(cbuf = new_array(char, MAX_DATA_COUNT))
317 || !(dbuf = new_array(char, AVAIL_OUT_SIZE(CHUNK_SIZE))))
Paul Mackerras5914bf11998-05-22 06:58:52 +0000318 out_of_memory("recv_deflated_token");
319 init_done = 1;
320 } else {
321 inflateReset(&rx_strm);
322 }
Paul Mackerrasb8d45241996-11-06 04:49:53 +0000323 recv_state = r_idle;
Paul Mackerras5914bf11998-05-22 06:58:52 +0000324 rx_token = 0;
325 break;
326
327 case r_idle:
328 case r_inflated:
329 if (saved_flag) {
330 flag = saved_flag & 0xff;
331 saved_flag = 0;
332 } else
333 flag = read_byte(f);
334 if ((flag & 0xC0) == DEFLATED_DATA) {
335 n = ((flag & 0x3f) << 8) + read_byte(f);
336 read_buf(f, cbuf, n);
337 rx_strm.next_in = (Bytef *)cbuf;
338 rx_strm.avail_in = n;
339 recv_state = r_inflating;
340 break;
341 }
342 if (recv_state == r_inflated) {
343 /* check previous inflated stuff ended correctly */
344 rx_strm.avail_in = 0;
345 rx_strm.next_out = (Bytef *)dbuf;
Wayne Davison1dbb94c2003-08-20 10:37:53 +0000346 rx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000347 r = inflate(&rx_strm, Z_SYNC_FLUSH);
Wayne Davison1dbb94c2003-08-20 10:37:53 +0000348 n = AVAIL_OUT_SIZE(CHUNK_SIZE) - rx_strm.avail_out;
Paul Mackerras5914bf11998-05-22 06:58:52 +0000349 /*
350 * Z_BUF_ERROR just means no progress was
351 * made, i.e. the decompressor didn't have
352 * any pending output for us.
353 */
354 if (r != Z_OK && r != Z_BUF_ERROR) {
355 rprintf(FERROR, "inflate flush returned %d (%d bytes)\n",
356 r, n);
Andrew Tridgell65417571998-11-03 07:08:27 +0000357 exit_cleanup(RERR_STREAMIO);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000358 }
359 if (n != 0 && r != Z_BUF_ERROR) {
360 /* have to return some more data and
361 save the flag for later. */
362 saved_flag = flag + 0x10000;
363 *data = dbuf;
364 return n;
365 }
366 /*
367 * At this point the decompressor should
368 * be expecting to see the 0, 0, ff, ff bytes.
369 */
370 if (!inflateSyncPoint(&rx_strm)) {
371 rprintf(FERROR, "decompressor lost sync!\n");
Andrew Tridgell65417571998-11-03 07:08:27 +0000372 exit_cleanup(RERR_STREAMIO);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000373 }
374 rx_strm.avail_in = 4;
375 rx_strm.next_in = (Bytef *)cbuf;
376 cbuf[0] = cbuf[1] = 0;
377 cbuf[2] = cbuf[3] = 0xff;
378 inflate(&rx_strm, Z_SYNC_FLUSH);
379 recv_state = r_idle;
380 }
381 if (flag == END_FLAG) {
382 /* that's all folks */
383 recv_state = r_init;
384 return 0;
385 }
386
387 /* here we have a token of some kind */
388 if (flag & TOKEN_REL) {
389 rx_token += flag & 0x3f;
390 flag >>= 6;
391 } else
392 rx_token = read_int(f);
393 if (flag & 1) {
394 rx_run = read_byte(f);
395 rx_run += read_byte(f) << 8;
396 recv_state = r_running;
397 }
398 return -1 - rx_token;
399
400 case r_inflating:
401 rx_strm.next_out = (Bytef *)dbuf;
Wayne Davison1dbb94c2003-08-20 10:37:53 +0000402 rx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000403 r = inflate(&rx_strm, Z_NO_FLUSH);
Wayne Davison1dbb94c2003-08-20 10:37:53 +0000404 n = AVAIL_OUT_SIZE(CHUNK_SIZE) - rx_strm.avail_out;
Paul Mackerras5914bf11998-05-22 06:58:52 +0000405 if (r != Z_OK) {
406 rprintf(FERROR, "inflate returned %d (%d bytes)\n", r, n);
Andrew Tridgell65417571998-11-03 07:08:27 +0000407 exit_cleanup(RERR_STREAMIO);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000408 }
409 if (rx_strm.avail_in == 0)
410 recv_state = r_inflated;
411 if (n != 0) {
412 *data = dbuf;
413 return n;
414 }
415 break;
416
417 case r_running:
418 ++rx_token;
419 if (--rx_run == 0)
420 recv_state = r_idle;
421 return -1 - rx_token;
Paul Mackerrasb8d45241996-11-06 04:49:53 +0000422 }
Paul Mackerras861c20b1996-07-03 04:05:46 +0000423 }
Paul Mackerras861c20b1996-07-03 04:05:46 +0000424}
425
426/*
427 * put the data corresponding to a token that we've just returned
428 * from recv_deflated_token into the decompressor's history buffer.
429 */
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000430static void see_deflate_token(char *buf, int32 len)
Paul Mackerras861c20b1996-07-03 04:05:46 +0000431{
Wayne Davisonacc461c2005-02-14 08:19:32 +0000432 int r;
433 int32 blklen;
Paul Mackerras5914bf11998-05-22 06:58:52 +0000434 unsigned char hdr[5];
Paul Mackerras861c20b1996-07-03 04:05:46 +0000435
Paul Mackerras5914bf11998-05-22 06:58:52 +0000436 rx_strm.avail_in = 0;
437 blklen = 0;
438 hdr[0] = 0;
439 do {
440 if (rx_strm.avail_in == 0 && len != 0) {
441 if (blklen == 0) {
442 /* Give it a fake stored-block header. */
443 rx_strm.next_in = (Bytef *)hdr;
444 rx_strm.avail_in = 5;
445 blklen = len;
446 if (blklen > 0xffff)
447 blklen = 0xffff;
448 hdr[1] = blklen;
449 hdr[2] = blklen >> 8;
450 hdr[3] = ~hdr[1];
451 hdr[4] = ~hdr[2];
452 } else {
453 rx_strm.next_in = (Bytef *)buf;
454 rx_strm.avail_in = blklen;
455 len -= blklen;
456 blklen = 0;
457 }
458 }
459 rx_strm.next_out = (Bytef *)dbuf;
Wayne Davison1dbb94c2003-08-20 10:37:53 +0000460 rx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000461 r = inflate(&rx_strm, Z_SYNC_FLUSH);
462 if (r != Z_OK) {
463 rprintf(FERROR, "inflate (token) returned %d\n", r);
Andrew Tridgell65417571998-11-03 07:08:27 +0000464 exit_cleanup(RERR_STREAMIO);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000465 }
466 } while (len || rx_strm.avail_out == 0);
Paul Mackerras861c20b1996-07-03 04:05:46 +0000467}
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000468
Martin Pool79f671c2002-04-08 08:35:30 +0000469/**
470 * Transmit a verbatim buffer of length @p n followed by a token.
Wayne Davisond67c8bd2004-06-18 16:22:14 +0000471 * If token == -1 then we have reached EOF
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000472 * If n == 0 then don't send a buffer
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000473 */
Wayne Davisonacc461c2005-02-14 08:19:32 +0000474void send_token(int f, int32 token, struct map_struct *buf, OFF_T offset,
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000475 int32 n, int32 toklen)
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000476{
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000477 if (!do_compression)
478 simple_send_token(f, token, buf, offset, n);
479 else
Paul Mackerras5914bf11998-05-22 06:58:52 +0000480 send_deflated_token(f, token, buf, offset, n, toklen);
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000481}
482
483
484/*
485 * receive a token or buffer from the other end. If the reurn value is >0 then
486 * it is a data buffer of that length, and *data will point at the data.
487 * if the return value is -i then it represents token i-1
488 * if the return value is 0 then the end has been reached
489 */
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000490int32 recv_token(int f, char **data)
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000491{
Paul Mackerras5914bf11998-05-22 06:58:52 +0000492 int tok;
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000493
Paul Mackerras5914bf11998-05-22 06:58:52 +0000494 if (!do_compression) {
495 tok = simple_recv_token(f,data);
496 } else {
497 tok = recv_deflated_token(f, data);
498 }
499 return tok;
Paul Mackerras861c20b1996-07-03 04:05:46 +0000500}
501
502/*
503 * look at the data corresponding to a token, if necessary
504 */
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000505void see_token(char *data, int32 toklen)
Paul Mackerras861c20b1996-07-03 04:05:46 +0000506{
Paul Mackerras5914bf11998-05-22 06:58:52 +0000507 if (do_compression)
508 see_deflate_token(data, toklen);
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000509}