Line data Source code
1 : #include "../../burp.h"
2 : #include "rabin.h"
3 : #include "rconf.h"
4 : #include "win.h"
5 : #include "../../alloc.h"
6 : #include "../blk.h"
7 : #include "../blist.h"
8 : #include "../../sbuf.h"
9 :
10 : static struct blk *blk=NULL;
11 : static char *gcp=NULL;
12 : static char *gbuf=NULL;
13 : static char *gbuf_end=NULL;
14 : static struct rconf rconf;
15 : static struct win *win=NULL; // Rabin sliding window.
16 : static int first=0;
17 :
18 7 : int blks_generate_init(void)
19 : {
20 7 : rconf_init(&rconf);
21 14 : if(!(win=win_alloc(&rconf))
22 7 : || !(gbuf=(char *)malloc_w(rconf.blk_max, __func__)))
23 : return -1;
24 7 : gbuf_end=gbuf;
25 7 : gcp=gbuf;
26 7 : return 0;
27 : }
28 :
29 8 : void blks_generate_free(void)
30 : {
31 8 : free_w(&gbuf);
32 8 : win_free(&win);
33 8 : }
34 :
35 : // This is where the magic happens.
36 : // Return 1 for got a block, 0 for no block got.
37 19 : static int blk_read(void)
38 : {
39 : char c;
40 :
41 38 : for(; gcp<gbuf_end; gcp++)
42 : {
43 19 : c=*gcp;
44 :
45 19 : blk->fingerprint = (blk->fingerprint * rconf.prime) + c;
46 19 : win->checksum = (win->checksum * rconf.prime) + c
47 19 : - (win->data[win->pos] * rconf.multiplier);
48 19 : win->data[win->pos] = c;
49 :
50 19 : win->pos++;
51 19 : if(blk->data) blk->data[blk->length] = c;
52 19 : blk->length++;
53 :
54 19 : if(win->pos == rconf.win_size) win->pos=0;
55 :
56 19 : if( blk->length >= rconf.blk_min
57 0 : && (blk->length == rconf.blk_max
58 0 : || (win->checksum % rconf.blk_avg) == rconf.prime))
59 : {
60 0 : gcp++;
61 0 : return 1;
62 : }
63 : }
64 : return 0;
65 : }
66 :
67 19 : static int blk_read_to_list(struct sbuf *sb, struct blist *blist)
68 : {
69 19 : if(!blk_read()) return 0;
70 :
71 : // Got something.
72 0 : if(first)
73 : {
74 0 : sb->protocol2->bstart=blk;
75 0 : first=0;
76 : }
77 0 : if(!sb->protocol2->bsighead)
78 : {
79 0 : sb->protocol2->bsighead=blk;
80 : }
81 0 : blist_add_blk(blist, blk);
82 0 : blk=NULL;
83 : return 1;
84 : }
85 :
86 : // The client uses this.
87 : // Return 0 for OK. 1 for OK, and file ended, -1 for error.
88 38 : int blks_generate(struct asfd *asfd, struct conf **confs,
89 : struct sbuf *sb, struct blist *blist, int just_opened)
90 : {
91 : static ssize_t bytes;
92 38 : first=just_opened;
93 :
94 38 : if(!blk && !(blk=blk_alloc_with_data(rconf.blk_max)))
95 : return -1;
96 :
97 38 : if(gcp<gbuf_end)
98 : {
99 : // Could have got a fill before buf ran out -
100 : // need to resume from the same place in that case.
101 0 : if(blk_read_to_list(sb, blist))
102 : return 0; // Got a block.
103 : // Did not get a block. Carry on and read more.
104 : }
105 38 : while((bytes=sbuf_read(sb, gbuf, rconf.blk_max)))
106 : {
107 19 : gcp=gbuf;
108 19 : gbuf_end=gbuf+bytes;
109 19 : sb->protocol2->bytes_read+=bytes;
110 19 : if(blk_read_to_list(sb, blist))
111 : return 0; // Got a block
112 : // Did not get a block. Maybe should try again?
113 : // If there are async timeouts, look at this!
114 : return 0;
115 : }
116 :
117 : // Getting here means there is no more to read from the file.
118 : // Make sure to deal with anything left over.
119 :
120 19 : if(!sb->protocol2->bytes_read)
121 : {
122 : // Empty file, set up an empty block so that the server
123 : // can skip over it.
124 0 : if(!(sb->protocol2->bstart=blk_alloc())) return -1;
125 0 : sb->protocol2->bsighead=blk;
126 0 : blist_add_blk(blist, blk);
127 0 : blk=NULL;
128 : }
129 19 : else if(blk)
130 : {
131 19 : if(blk->length)
132 : {
133 19 : if(first)
134 : {
135 0 : sb->protocol2->bstart=blk;
136 0 : first=0;
137 : }
138 19 : if(!sb->protocol2->bsighead)
139 : {
140 19 : sb->protocol2->bsighead=blk;
141 : }
142 19 : blist_add_blk(blist, blk);
143 : }
144 0 : else blk_free(&blk);
145 19 : blk=NULL;
146 : }
147 19 : if(blist->tail) sb->protocol2->bend=blist->tail;
148 : return 1;
149 : }
150 :
151 : // The server uses this for verification.
152 0 : int blk_read_verify(struct blk *blk_to_verify)
153 : {
154 0 : if(!win)
155 : {
156 0 : rconf_init(&rconf);
157 0 : if(!(win=win_alloc(&rconf))) return -1;
158 : }
159 :
160 0 : gbuf=blk_to_verify->data;
161 0 : gbuf_end=gbuf+blk_to_verify->length;
162 0 : gcp=gbuf;
163 0 : if(!blk && !(blk=blk_alloc())) return -1;
164 0 : blk->length=0;
165 0 : blk->fingerprint=0;
166 :
167 : // FIX THIS: blk_read should return 1 when it has a block.
168 : // But, if the block is too small (because the end of the file
169 : // happened), it returns 0, and blks_generate treats it as having found
170 : // a final block.
171 : // So, here the return of blk_read is ignored and we look at the
172 : // position of gcp instead.
173 0 : blk_read();
174 0 : if(gcp==gbuf_end
175 0 : && blk->fingerprint==blk_to_verify->fingerprint)
176 : return 1;
177 0 : return 0;
178 : }
|