added hooks for compression in token.c
[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"
21
22extern int do_compression;
23
24
25/* non-compressing recv token */
26static int simple_recv_token(int f,char **data)
27{
28 static int residue = 0;
29 static char *buf = NULL;
30 int n;
31
32 if (!buf) {
33 buf = (char *)malloc(CHUNK_SIZE);
34 if (!buf) out_of_memory("simple_recv_token");
35 }
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 */
53static void simple_send_token(int f,int token,char *buf,int offset,int n)
54{
55 if (n > 0) {
56 int l = 0;
57 while (l < n) {
58 int n1 = MIN(CHUNK_SIZE,n-l);
59 write_int(f,n1);
60 write_buf(f,map_ptr(buf,offset+l,n1),n1);
61 l += n1;
62 }
63 }
64 write_int(f,-(token+1));
65}
66
67
68
69
70/*
71 * transmit a verbatim buffer of length n followed by a token
72 * If token == -1 then we have reached EOF
73 * If n == 0 then don't send a buffer
74 * Note that "buf" must be used via map_ptr() starting at "offset"
75 */
76void send_token(int f,int token,char *buf,int offset,int n)
77{
78 if (!do_compression) {
79 simple_send_token(f,token,buf,offset,n);
80 return;
81 }
82
83 /* compressed transmit here */
84}
85
86
87/*
88 * receive a token or buffer from the other end. If the reurn value is >0 then
89 * it is a data buffer of that length, and *data will point at the data.
90 * if the return value is -i then it represents token i-1
91 * if the return value is 0 then the end has been reached
92 */
93int recv_token(int f,char **data)
94{
95 if (!do_compression) {
96 return simple_recv_token(f,data);
97 }
98
99 /* compressed receive here */
100 return 0;
101}