formatting changes. committed separately so they don't mask the coming
[rsync/rsync.git] / token.c
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"
21 #include "lib/zlib.h"
22
23 extern int do_compression;
24
25
26 /* non-compressing recv token */
27 static int simple_recv_token(int f,char **data)
28 {
29         static int residue;
30         static char *buf;
31         int n;
32
33         if (!buf) {
34                 buf = (char *)malloc(CHUNK_SIZE);
35                 if (!buf) out_of_memory("simple_recv_token");
36         }
37
38         if (residue == 0) {
39                 int i = read_int(f);
40                 if (i <= 0) return i;
41                 residue = i;
42         }
43
44         *data = buf;
45         n = MIN(CHUNK_SIZE,residue);
46         residue -= n;
47         read_buf(f,buf,n);
48         return n;
49 }
50
51
52 /* non-compressing send token */
53 static void simple_send_token(int f,int token,
54                               struct map_struct *buf,int offset,int n)
55 {
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         }
65         write_int(f,-(token+1));
66 }
67
68
69 /* Memory allocation/freeing routines, called by zlib stuff. */
70 static void *
71 z_alloc(void *opaque, uInt items, uInt size)
72 {
73     return malloc(items * size);
74 }
75
76 static void
77 z_free(void *opaque, void *adrs, uInt nbytes)
78 {
79     free(adrs);
80 }
81
82 /* Flag bytes in compressed stream are encoded as follows: */
83 #define END_FLAG        0       /* that's all folks */
84 #define TOKEN_LONG      0x20    /* followed by 32-bit token number */
85 #define TOKENRUN_LONG   0x21    /* ditto with 16-bit run count */
86 #define DEFLATED_DATA   0x40    /* + 6-bit high len, then low len byte */
87 #define TOKEN_REL       0x80    /* + 6-bit relative token number */
88 #define TOKENRUN_REL    0xc0    /* ditto with 16-bit run count */
89
90 #define MAX_DATA_COUNT  16383   /* fit 14 bit count into 2 bytes with flags */
91
92 /* For coding runs of tokens */
93 static int last_token = -1;
94 static int run_start;
95 static int last_run_end;
96
97 /* Deflation state */
98 static z_stream tx_strm;
99
100 /* Output buffer */
101 static char *obuf;
102
103 /* Send a deflated token */
104 static void
105 send_deflated_token(int f, int token,
106                     struct map_struct *buf, int offset, int nb, int toklen)
107 {
108     int n, r;
109     static int init_done;
110
111     if (last_token == -1) {
112         /* initialization */
113         if (!init_done) {
114             tx_strm.next_in = NULL;
115             tx_strm.zalloc = z_alloc;
116             tx_strm.zfree = z_free;
117             if (deflateInit2(&tx_strm, Z_DEFAULT_COMPRESSION, 8,
118                              -15, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
119                 rprintf(FERROR, "compression init failed\n");
120                 exit_cleanup(1);
121             }
122             if ((obuf = malloc(MAX_DATA_COUNT+2)) == NULL)
123                 out_of_memory("send_deflated_token");
124             init_done = 1;
125         } else
126             deflateReset(&tx_strm);
127         run_start = token;
128         last_run_end = 0;
129
130     } else if (nb != 0 || token != last_token + 1
131                || token >= run_start + 65536) {
132         /* output previous run */
133         r = run_start - last_run_end;
134         n = last_token - run_start;
135         if (r >= 0 && r <= 63) {
136             write_byte(f, (n==0? TOKEN_REL: TOKENRUN_REL) + r);
137         } else {
138             write_byte(f, (n==0? TOKEN_LONG: TOKENRUN_LONG));
139             write_int(f, run_start);
140         }
141         if (n != 0) {
142             write_byte(f, n);
143             write_byte(f, n >> 8);
144         }
145         last_run_end = last_token;
146         run_start = token;
147     }
148
149     last_token = token;
150
151     if (nb != 0) {
152         /* deflate the data starting at offset */
153         tx_strm.avail_in = 0;
154         tx_strm.avail_out = 0;
155         do {
156             if (tx_strm.avail_in == 0 && nb != 0) {
157                 /* give it some more input */
158                 n = MIN(nb, CHUNK_SIZE);
159                 tx_strm.next_in = (Bytef *)map_ptr(buf, offset, n);
160                 tx_strm.avail_in = n;
161                 nb -= n;
162                 offset += n;
163             }
164             if (tx_strm.avail_out == 0) {
165                 tx_strm.next_out = (Bytef *)(obuf + 2);
166                 tx_strm.avail_out = MAX_DATA_COUNT;
167             }
168             r = deflate(&tx_strm, nb? Z_NO_FLUSH: Z_PACKET_FLUSH);
169             if (r != Z_OK) {
170                 rprintf(FERROR, "deflate returned %d\n", r);
171                 exit_cleanup(1);
172             }
173             if (nb == 0 || tx_strm.avail_out == 0) {
174                 n = MAX_DATA_COUNT - tx_strm.avail_out;
175                 if (n > 0) {
176                     obuf[0] = DEFLATED_DATA + (n >> 8);
177                     obuf[1] = n;
178                     write_buf(f, obuf, n+2);
179                 }
180             }
181         } while (nb != 0 || tx_strm.avail_out == 0);
182     }
183
184     if (token != -1) {
185         /* add the data in the current block to the compressor's
186            history and hash table */
187         tx_strm.next_in = (Bytef *)map_ptr(buf, offset, toklen);
188         tx_strm.avail_in = toklen;
189         tx_strm.next_out = NULL;
190         tx_strm.avail_out = 2 * toklen;
191         r = deflate(&tx_strm, Z_INSERT_ONLY);
192         if (r != Z_OK || tx_strm.avail_in != 0) {
193             rprintf(FERROR, "deflate on token returned %d (%d bytes left)\n",
194                     r, tx_strm.avail_in);
195             exit_cleanup(1);
196         }
197
198     } else {
199         /* end of file - clean up */
200         write_byte(f, END_FLAG);
201     }
202 }
203
204
205 /* tells us what the receiver is in the middle of doing */
206 static enum { r_init, r_idle, r_running, r_inflating, r_inflated } recv_state;
207
208 /* for inflating stuff */
209 static z_stream rx_strm;
210 static char *cbuf;
211 static char *dbuf;
212
213 /* for decoding runs of tokens */
214 static int rx_token;
215 static int rx_run;
216
217 /* Receive a deflated token and inflate it */
218 static int
219 recv_deflated_token(int f, char **data)
220 {
221     int n, r, flag;
222     static int init_done;
223     static int saved_flag;
224
225     for (;;) {
226         switch (recv_state) {
227         case r_init:
228             if (!init_done) {
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                     rprintf(FERROR, "inflate init failed\n");
234                     exit_cleanup(1);
235                 }
236                 if ((cbuf = malloc(MAX_DATA_COUNT)) == NULL
237                     || (dbuf = malloc(CHUNK_SIZE)) == NULL)
238                     out_of_memory("recv_deflated_token");
239                 init_done = 1;
240             } else {
241                 inflateReset(&rx_strm);
242             }
243             recv_state = r_idle;
244             rx_token = 0;
245             break;
246             
247         case r_idle:
248         case r_inflated:
249             if (saved_flag) {
250                 flag = saved_flag & 0xff;
251                 saved_flag = 0;
252             } else
253                 flag = read_byte(f);
254             if ((flag & 0xC0) == DEFLATED_DATA) {
255                 n = ((flag & 0x3f) << 8) + read_byte(f);
256                 read_buf(f, cbuf, n);
257                 rx_strm.next_in = (Bytef *)cbuf;
258                 rx_strm.avail_in = n;
259                 recv_state = r_inflating;
260                 break;
261             }
262             if (recv_state == r_inflated) {
263                 /* check previous inflated stuff ended correctly */
264                 rx_strm.avail_in = 0;
265                 rx_strm.next_out = (Bytef *)dbuf;
266                 rx_strm.avail_out = CHUNK_SIZE;
267                 r = inflate(&rx_strm, Z_PACKET_FLUSH);
268                 n = CHUNK_SIZE - rx_strm.avail_out;
269                 if (r != Z_OK) {
270                     rprintf(FERROR, "inflate flush returned %d (%d bytes)\n",
271                             r, n);
272                     exit_cleanup(1);
273                 }
274                 if (n != 0) {
275                     /* have to return some more data and
276                        save the flag for later. */
277                     saved_flag = flag + 0x10000;
278                     if (rx_strm.avail_out != 0)
279                         recv_state = r_idle;
280                     *data = dbuf;
281                     return n;
282                 }
283                 recv_state = r_idle;
284             }
285             if (flag == END_FLAG) {
286                 /* that's all folks */
287                 recv_state = r_init;
288                 return 0;
289             }
290
291             /* here we have a token of some kind */
292             if (flag & TOKEN_REL) {
293                 rx_token += flag & 0x3f;
294                 flag >>= 6;
295             } else
296                 rx_token = read_int(f);
297             if (flag & 1) {
298                 rx_run = read_byte(f);
299                 rx_run += read_byte(f) << 8;
300                 recv_state = r_running;
301             }
302             return -1 - rx_token;
303
304         case r_inflating:
305             rx_strm.next_out = (Bytef *)dbuf;
306             rx_strm.avail_out = CHUNK_SIZE;
307             r = inflate(&rx_strm, Z_NO_FLUSH);
308             n = CHUNK_SIZE - rx_strm.avail_out;
309             if (r != Z_OK) {
310                 rprintf(FERROR, "inflate returned %d (%d bytes)\n", r, n);
311                 exit_cleanup(1);
312             }
313             if (rx_strm.avail_in == 0)
314                 recv_state = r_inflated;
315             if (n != 0) {
316                 *data = dbuf;
317                 return n;
318             }
319             break;
320
321         case r_running:
322             ++rx_token;
323             if (--rx_run == 0)
324                 recv_state = r_idle;
325             return -1 - rx_token;
326         }
327     }
328 }
329
330 /*
331  * put the data corresponding to a token that we've just returned
332  * from recv_deflated_token into the decompressor's history buffer.
333  */
334 void
335 see_deflate_token(char *buf, int len)
336 {
337     int r;
338
339     rx_strm.next_in = (Bytef *)buf;
340     rx_strm.avail_in = len;
341     r = inflateIncomp(&rx_strm);
342     if (r != Z_OK) {
343         rprintf(FERROR, "inflateIncomp returned %d\n", r);
344         exit_cleanup(1);
345     }
346 }
347
348 /*
349  * transmit a verbatim buffer of length n followed by a token 
350  * If token == -1 then we have reached EOF 
351  * If n == 0 then don't send a buffer
352  */
353 void send_token(int f,int token,struct map_struct *buf,int offset,
354                 int n,int toklen)
355 {
356   if (!do_compression) {
357     simple_send_token(f,token,buf,offset,n);
358   } else {
359     send_deflated_token(f, token, buf, offset, n, toklen);
360   }
361 }
362
363
364 /*
365  * receive a token or buffer from the other end. If the reurn value is >0 then
366  * it is a data buffer of that length, and *data will point at the data.
367  * if the return value is -i then it represents token i-1
368  * if the return value is 0 then the end has been reached
369  */
370 int recv_token(int f,char **data)
371 {
372   int tok;
373
374   if (!do_compression) {
375     tok = simple_recv_token(f,data);
376   } else {
377     tok = recv_deflated_token(f, data);
378   }
379   return tok;
380 }
381
382 /*
383  * look at the data corresponding to a token, if necessary
384  */
385 void see_token(char *data, int toklen)
386 {
387     if (do_compression)
388         see_deflate_token(data, toklen);
389 }