improved the "refuse options" code a bit
[rsync/rsync.git] / token.c
CommitLineData
70d794dc
AT
1/*
2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
4
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.
9
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.
14
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.
18*/
19
20#include "rsync.h"
5914bf15 21#include "zlib/zlib.h"
70d794dc
AT
22
23extern int do_compression;
24
25
26/* non-compressing recv token */
27static int simple_recv_token(int f,char **data)
28{
c5eb3650
AT
29 static int residue;
30 static char *buf;
31 int n;
70d794dc 32
c5eb3650
AT
33 if (!buf) {
34 buf = (char *)malloc(CHUNK_SIZE);
35 if (!buf) out_of_memory("simple_recv_token");
36 }
70d794dc 37
c5eb3650
AT
38 if (residue == 0) {
39 int i = read_int(f);
40 if (i <= 0) return i;
41 residue = i;
42 }
70d794dc 43
c5eb3650
AT
44 *data = buf;
45 n = MIN(CHUNK_SIZE,residue);
46 residue -= n;
47 read_buf(f,buf,n);
48 return n;
70d794dc
AT
49}
50
51
52/* non-compressing send token */
c6e7fcb4 53static void simple_send_token(int f,int token,
45f133b9 54 struct map_struct *buf,OFF_T offset,int n)
70d794dc 55{
c5eb3650
AT
56 if (n > 0) {
57 int l = 0;
58 while (l < n) {
59 int n1 = MIN(CHUNK_SIZE,n-l);
60 write_int(f,n1);
61 write_buf(f,map_ptr(buf,offset+l,n1),n1);
62 l += n1;
63 }
64 }
45f133b9
AT
65 /* a -2 token means to send data only and no token */
66 if (token != -2) {
67 write_int(f,-(token+1));
68 }
70d794dc
AT
69}
70
71
861c20b4
PM
72/* Flag bytes in compressed stream are encoded as follows: */
73#define END_FLAG 0 /* that's all folks */
74#define TOKEN_LONG 0x20 /* followed by 32-bit token number */
75#define TOKENRUN_LONG 0x21 /* ditto with 16-bit run count */
76#define DEFLATED_DATA 0x40 /* + 6-bit high len, then low len byte */
77#define TOKEN_REL 0x80 /* + 6-bit relative token number */
78#define TOKENRUN_REL 0xc0 /* ditto with 16-bit run count */
79
80#define MAX_DATA_COUNT 16383 /* fit 14 bit count into 2 bytes with flags */
81
82/* For coding runs of tokens */
83static int last_token = -1;
84static int run_start;
85static int last_run_end;
86
87/* Deflation state */
88static z_stream tx_strm;
89
90/* Output buffer */
3a6a366f 91static char *obuf;
861c20b4
PM
92
93/* Send a deflated token */
94static void
95send_deflated_token(int f, int token,
45f133b9 96 struct map_struct *buf, OFF_T offset, int nb, int toklen)
861c20b4 97{
5914bf15
PM
98 int n, r;
99 static int init_done, flush_pending;
100
101 if (last_token == -1) {
102 /* initialization */
103 if (!init_done) {
104 tx_strm.next_in = NULL;
105 tx_strm.zalloc = NULL;
106 tx_strm.zfree = NULL;
107 if (deflateInit2(&tx_strm, Z_DEFAULT_COMPRESSION,
108 Z_DEFLATED, -15, 8,
109 Z_DEFAULT_STRATEGY) != Z_OK) {
110 rprintf(FERROR, "compression init failed\n");
65417579 111 exit_cleanup(RERR_STREAMIO);
5914bf15
PM
112 }
113 if ((obuf = malloc(MAX_DATA_COUNT+2)) == NULL)
114 out_of_memory("send_deflated_token");
115 init_done = 1;
116 } else
117 deflateReset(&tx_strm);
118 last_run_end = 0;
119 run_start = token;
120 flush_pending = 0;
121
122 } else if (last_token == -2) {
123 run_start = token;
124
125 } else if (nb != 0 || token != last_token + 1
126 || token >= run_start + 65536) {
127 /* output previous run */
128 r = run_start - last_run_end;
129 n = last_token - run_start;
130 if (r >= 0 && r <= 63) {
131 write_byte(f, (n==0? TOKEN_REL: TOKENRUN_REL) + r);
132 } else {
133 write_byte(f, (n==0? TOKEN_LONG: TOKENRUN_LONG));
134 write_int(f, run_start);
135 }
136 if (n != 0) {
137 write_byte(f, n);
138 write_byte(f, n >> 8);
139 }
140 last_run_end = last_token;
141 run_start = token;
861c20b4 142 }
5914bf15
PM
143
144 last_token = token;
145
146 if (nb != 0 || flush_pending) {
147 /* deflate the data starting at offset */
148 int flush = Z_NO_FLUSH;
149 tx_strm.avail_in = 0;
150 tx_strm.avail_out = 0;
151 do {
152 if (tx_strm.avail_in == 0 && nb != 0) {
153 /* give it some more input */
154 n = MIN(nb, CHUNK_SIZE);
155 tx_strm.next_in = (Bytef *)
156 map_ptr(buf, offset, n);
157 tx_strm.avail_in = n;
158 nb -= n;
159 offset += n;
160 }
161 if (tx_strm.avail_out == 0) {
162 tx_strm.next_out = (Bytef *)(obuf + 2);
163 tx_strm.avail_out = MAX_DATA_COUNT;
164 if (flush != Z_NO_FLUSH) {
165 /*
166 * We left the last 4 bytes in the
167 * buffer, in case they are the
168 * last 4. Move them to the front.
169 */
170 memcpy(tx_strm.next_out,
171 obuf+MAX_DATA_COUNT-2, 4);
172 tx_strm.next_out += 4;
173 tx_strm.avail_out -= 4;
174 }
175 }
176 if (nb == 0 && token != -2)
177 flush = Z_SYNC_FLUSH;
178 r = deflate(&tx_strm, flush);
179 if (r != Z_OK) {
180 rprintf(FERROR, "deflate returned %d\n", r);
65417579 181 exit_cleanup(RERR_STREAMIO);
5914bf15
PM
182 }
183 if (nb == 0 || tx_strm.avail_out == 0) {
184 n = MAX_DATA_COUNT - tx_strm.avail_out;
185 if (flush != Z_NO_FLUSH) {
186 /*
187 * We have to trim off the last 4
188 * bytes of output when flushing
189 * (they are just 0, 0, ff, ff).
190 */
191 n -= 4;
192 }
193 if (n > 0) {
194 obuf[0] = DEFLATED_DATA + (n >> 8);
195 obuf[1] = n;
196 write_buf(f, obuf, n+2);
197 }
198 }
199 } while (nb != 0 || tx_strm.avail_out == 0);
200 flush_pending = token == -2;
861c20b4 201 }
861c20b4 202
5914bf15
PM
203 if (token == -1) {
204 /* end of file - clean up */
205 write_byte(f, END_FLAG);
861c20b4 206
5914bf15
PM
207 } else if (token != -2) {
208 /* add the data in the current block to the compressor's
209 history and hash table */
210 tx_strm.next_in = (Bytef *) map_ptr(buf, offset, toklen);
211 tx_strm.avail_in = toklen;
212 tx_strm.next_out = (Bytef *) obuf;
861c20b4 213 tx_strm.avail_out = MAX_DATA_COUNT;
5914bf15
PM
214 r = deflate(&tx_strm, Z_INSERT_ONLY);
215 if (r != Z_OK || tx_strm.avail_in != 0) {
216 rprintf(FERROR, "deflate on token returned %d (%d bytes left)\n",
217 r, tx_strm.avail_in);
65417579 218 exit_cleanup(RERR_STREAMIO);
861c20b4 219 }
861c20b4 220 }
861c20b4
PM
221}
222
223
224/* tells us what the receiver is in the middle of doing */
225static enum { r_init, r_idle, r_running, r_inflating, r_inflated } recv_state;
70d794dc 226
861c20b4
PM
227/* for inflating stuff */
228static z_stream rx_strm;
229static char *cbuf;
230static char *dbuf;
231
232/* for decoding runs of tokens */
233static int rx_token;
234static int rx_run;
235
236/* Receive a deflated token and inflate it */
237static int
238recv_deflated_token(int f, char **data)
239{
5914bf15
PM
240 int n, r, flag;
241 static int init_done;
242 static int saved_flag;
243
244 for (;;) {
245 switch (recv_state) {
246 case r_init:
247 if (!init_done) {
248 rx_strm.next_out = NULL;
249 rx_strm.zalloc = NULL;
250 rx_strm.zfree = NULL;
251 if (inflateInit2(&rx_strm, -15) != Z_OK) {
252 rprintf(FERROR, "inflate init failed\n");
65417579 253 exit_cleanup(RERR_STREAMIO);
5914bf15
PM
254 }
255 if ((cbuf = malloc(MAX_DATA_COUNT)) == NULL
256 || (dbuf = malloc(CHUNK_SIZE)) == NULL)
257 out_of_memory("recv_deflated_token");
258 init_done = 1;
259 } else {
260 inflateReset(&rx_strm);
261 }
b8d4524b 262 recv_state = r_idle;
5914bf15
PM
263 rx_token = 0;
264 break;
265
266 case r_idle:
267 case r_inflated:
268 if (saved_flag) {
269 flag = saved_flag & 0xff;
270 saved_flag = 0;
271 } else
272 flag = read_byte(f);
273 if ((flag & 0xC0) == DEFLATED_DATA) {
274 n = ((flag & 0x3f) << 8) + read_byte(f);
275 read_buf(f, cbuf, n);
276 rx_strm.next_in = (Bytef *)cbuf;
277 rx_strm.avail_in = n;
278 recv_state = r_inflating;
279 break;
280 }
281 if (recv_state == r_inflated) {
282 /* check previous inflated stuff ended correctly */
283 rx_strm.avail_in = 0;
284 rx_strm.next_out = (Bytef *)dbuf;
285 rx_strm.avail_out = CHUNK_SIZE;
286 r = inflate(&rx_strm, Z_SYNC_FLUSH);
287 n = CHUNK_SIZE - rx_strm.avail_out;
288 /*
289 * Z_BUF_ERROR just means no progress was
290 * made, i.e. the decompressor didn't have
291 * any pending output for us.
292 */
293 if (r != Z_OK && r != Z_BUF_ERROR) {
294 rprintf(FERROR, "inflate flush returned %d (%d bytes)\n",
295 r, n);
65417579 296 exit_cleanup(RERR_STREAMIO);
5914bf15
PM
297 }
298 if (n != 0 && r != Z_BUF_ERROR) {
299 /* have to return some more data and
300 save the flag for later. */
301 saved_flag = flag + 0x10000;
302 *data = dbuf;
303 return n;
304 }
305 /*
306 * At this point the decompressor should
307 * be expecting to see the 0, 0, ff, ff bytes.
308 */
309 if (!inflateSyncPoint(&rx_strm)) {
310 rprintf(FERROR, "decompressor lost sync!\n");
65417579 311 exit_cleanup(RERR_STREAMIO);
5914bf15
PM
312 }
313 rx_strm.avail_in = 4;
314 rx_strm.next_in = (Bytef *)cbuf;
315 cbuf[0] = cbuf[1] = 0;
316 cbuf[2] = cbuf[3] = 0xff;
317 inflate(&rx_strm, Z_SYNC_FLUSH);
318 recv_state = r_idle;
319 }
320 if (flag == END_FLAG) {
321 /* that's all folks */
322 recv_state = r_init;
323 return 0;
324 }
325
326 /* here we have a token of some kind */
327 if (flag & TOKEN_REL) {
328 rx_token += flag & 0x3f;
329 flag >>= 6;
330 } else
331 rx_token = read_int(f);
332 if (flag & 1) {
333 rx_run = read_byte(f);
334 rx_run += read_byte(f) << 8;
335 recv_state = r_running;
336 }
337 return -1 - rx_token;
338
339 case r_inflating:
340 rx_strm.next_out = (Bytef *)dbuf;
341 rx_strm.avail_out = CHUNK_SIZE;
342 r = inflate(&rx_strm, Z_NO_FLUSH);
343 n = CHUNK_SIZE - rx_strm.avail_out;
344 if (r != Z_OK) {
345 rprintf(FERROR, "inflate returned %d (%d bytes)\n", r, n);
65417579 346 exit_cleanup(RERR_STREAMIO);
5914bf15
PM
347 }
348 if (rx_strm.avail_in == 0)
349 recv_state = r_inflated;
350 if (n != 0) {
351 *data = dbuf;
352 return n;
353 }
354 break;
355
356 case r_running:
357 ++rx_token;
358 if (--rx_run == 0)
359 recv_state = r_idle;
360 return -1 - rx_token;
b8d4524b 361 }
861c20b4 362 }
861c20b4
PM
363}
364
365/*
366 * put the data corresponding to a token that we've just returned
367 * from recv_deflated_token into the decompressor's history buffer.
368 */
6e4fb64e 369static void see_deflate_token(char *buf, int len)
861c20b4 370{
5914bf15
PM
371 int r, blklen;
372 unsigned char hdr[5];
373
374 rx_strm.avail_in = 0;
375 blklen = 0;
376 hdr[0] = 0;
377 do {
378 if (rx_strm.avail_in == 0 && len != 0) {
379 if (blklen == 0) {
380 /* Give it a fake stored-block header. */
381 rx_strm.next_in = (Bytef *)hdr;
382 rx_strm.avail_in = 5;
383 blklen = len;
384 if (blklen > 0xffff)
385 blklen = 0xffff;
386 hdr[1] = blklen;
387 hdr[2] = blklen >> 8;
388 hdr[3] = ~hdr[1];
389 hdr[4] = ~hdr[2];
390 } else {
391 rx_strm.next_in = (Bytef *)buf;
392 rx_strm.avail_in = blklen;
393 len -= blklen;
394 blklen = 0;
395 }
396 }
397 rx_strm.next_out = (Bytef *)dbuf;
398 rx_strm.avail_out = CHUNK_SIZE;
399 r = inflate(&rx_strm, Z_SYNC_FLUSH);
400 if (r != Z_OK) {
401 rprintf(FERROR, "inflate (token) returned %d\n", r);
65417579 402 exit_cleanup(RERR_STREAMIO);
5914bf15
PM
403 }
404 } while (len || rx_strm.avail_out == 0);
861c20b4 405}
70d794dc
AT
406
407/*
408 * transmit a verbatim buffer of length n followed by a token
409 * If token == -1 then we have reached EOF
410 * If n == 0 then don't send a buffer
70d794dc 411 */
45f133b9 412void send_token(int f,int token,struct map_struct *buf,OFF_T offset,
9e31c482 413 int n,int toklen)
70d794dc 414{
5914bf15
PM
415 if (!do_compression) {
416 simple_send_token(f,token,buf,offset,n);
417 } else {
418 send_deflated_token(f, token, buf, offset, n, toklen);
419 }
70d794dc
AT
420}
421
422
423/*
424 * receive a token or buffer from the other end. If the reurn value is >0 then
425 * it is a data buffer of that length, and *data will point at the data.
426 * if the return value is -i then it represents token i-1
427 * if the return value is 0 then the end has been reached
428 */
429int recv_token(int f,char **data)
430{
5914bf15
PM
431 int tok;
432
433 if (!do_compression) {
434 tok = simple_recv_token(f,data);
435 } else {
436 tok = recv_deflated_token(f, data);
437 }
438 return tok;
861c20b4
PM
439}
440
441/*
442 * look at the data corresponding to a token, if necessary
443 */
444void see_token(char *data, int toklen)
445{
5914bf15
PM
446 if (do_compression)
447 see_deflate_token(data, toklen);
70d794dc 448}