author | kergoth <kergoth> | 2002-01-25 22:14:26 (UTC) |
---|---|---|
committer | kergoth <kergoth> | 2002-01-25 22:14:26 (UTC) |
commit | 15318cad33835e4e2dc620d033e43cd930676cdd (patch) (unidiff) | |
tree | c2fa0399a2c47fda8e2cd0092c73a809d17f68eb /rsync/readsums.c | |
download | opie-15318cad33835e4e2dc620d033e43cd930676cdd.zip opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.gz opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.bz2 |
Initial revision
-rw-r--r-- | rsync/readsums.c | 214 |
1 files changed, 214 insertions, 0 deletions
diff --git a/rsync/readsums.c b/rsync/readsums.c new file mode 100644 index 0000000..21b5ecd --- a/dev/null +++ b/rsync/readsums.c | |||
@@ -0,0 +1,214 @@ | |||
1 | /*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- | ||
2 | * | ||
3 | * librsync -- the library for network deltas | ||
4 | * $Id$ | ||
5 | * | ||
6 | * Copyright (C) 1999, 2000, 2001 by Martin Pool <mbp@samba.org> | ||
7 | * Copyright (C) 1999 by Andrew Tridgell <tridge@samba.org> | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or | ||
10 | * modify it under the terms of the GNU Lesser General Public License | ||
11 | * as published by the Free Software Foundation; either version 2.1 of | ||
12 | * the License, or (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, but | ||
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * Lesser General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU Lesser General Public | ||
20 | * License along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
22 | */ | ||
23 | |||
24 | |||
25 | /* | ||
26 | * readsums.c -- Load signatures from a file into an ::rs_signature_t. | ||
27 | */ | ||
28 | |||
29 | #include <config_rsync.h> | ||
30 | |||
31 | #include <assert.h> | ||
32 | #include <stdlib.h> | ||
33 | #include <stdio.h> | ||
34 | #include <string.h> | ||
35 | |||
36 | #include "rsync.h" | ||
37 | #include "sumset.h" | ||
38 | #include "job.h" | ||
39 | #include "trace.h" | ||
40 | #include "netint.h" | ||
41 | #include "protocol.h" | ||
42 | #include "util.h" | ||
43 | #include "stream.h" | ||
44 | |||
45 | |||
46 | static rs_result rs_loadsig_s_weak(rs_job_t *job); | ||
47 | static rs_result rs_loadsig_s_strong(rs_job_t *job); | ||
48 | |||
49 | |||
50 | |||
51 | /** | ||
52 | * Add a just-read-in checksum pair to the signature block. | ||
53 | */ | ||
54 | static rs_result rs_loadsig_add_sum(rs_job_t *job, rs_strong_sum_t *strong) | ||
55 | { | ||
56 | size_t new_size; | ||
57 | rs_signature_t *sig = job->signature; | ||
58 | rs_block_sig_t *asignature; | ||
59 | |||
60 | sig->count++; | ||
61 | new_size = sig->count * sizeof(rs_block_sig_t); | ||
62 | |||
63 | sig->block_sigs = realloc(sig->block_sigs, new_size); | ||
64 | |||
65 | if (sig->block_sigs == NULL) { | ||
66 | return RS_MEM_ERROR; | ||
67 | } | ||
68 | asignature = &(sig->block_sigs[sig->count - 1]); | ||
69 | |||
70 | asignature->weak_sum = job->weak_sig; | ||
71 | asignature->i = sig->count; | ||
72 | |||
73 | memcpy(asignature->strong_sum, strong, sig->strong_sum_len); | ||
74 | |||
75 | if (rs_trace_enabled()) { | ||
76 | char hexbuf[RS_MD4_LENGTH * 2 + 2]; | ||
77 | rs_hexify(hexbuf, strong, sig->strong_sum_len); | ||
78 | |||
79 | rs_trace("read in checksum: weak=%#x, strong=%s", asignature->weak_sum, | ||
80 | hexbuf); | ||
81 | } | ||
82 | |||
83 | job->stats.sig_blocks++; | ||
84 | |||
85 | return RS_RUNNING; | ||
86 | } | ||
87 | |||
88 | |||
89 | static rs_result rs_loadsig_s_weak(rs_job_t *job) | ||
90 | { | ||
91 | int l; | ||
92 | rs_result result; | ||
93 | |||
94 | result = rs_suck_n4(job, &l); | ||
95 | if (result == RS_DONE) | ||
96 | ; | ||
97 | else if (result == RS_INPUT_ENDED) /* ending here is OK */ | ||
98 | return RS_DONE; | ||
99 | else | ||
100 | return result; | ||
101 | |||
102 | job->weak_sig = l; | ||
103 | |||
104 | job->statefn = rs_loadsig_s_strong; | ||
105 | |||
106 | return RS_RUNNING; | ||
107 | } | ||
108 | |||
109 | |||
110 | |||
111 | static rs_result rs_loadsig_s_strong(rs_job_t *job) | ||
112 | { | ||
113 | rs_result result; | ||
114 | rs_strong_sum_t *strongsum; | ||
115 | |||
116 | result = rs_scoop_read(job, job->signature->strong_sum_len, | ||
117 | (void **) &strongsum); | ||
118 | if (result != RS_DONE) return result; | ||
119 | |||
120 | job->statefn = rs_loadsig_s_weak; | ||
121 | |||
122 | return rs_loadsig_add_sum(job, strongsum); | ||
123 | } | ||
124 | |||
125 | |||
126 | |||
127 | static rs_result rs_loadsig_s_stronglen(rs_job_t *job) | ||
128 | { | ||
129 | int l; | ||
130 | rs_result result; | ||
131 | |||
132 | if ((result = rs_suck_n4(job, &l)) != RS_DONE) | ||
133 | return result; | ||
134 | job->strong_sum_len = l; | ||
135 | |||
136 | if (l < 0 || l > RS_MD4_LENGTH) { | ||
137 | rs_error("strong sum length %d is implausible", l); | ||
138 | return RS_CORRUPT; | ||
139 | } | ||
140 | |||
141 | job->signature->block_len = job->block_len; | ||
142 | job->signature->strong_sum_len = job->strong_sum_len; | ||
143 | |||
144 | rs_trace("allocated sigset_t (strong_sum_len=%d, block_len=%d)", | ||
145 | (int) job->strong_sum_len, (int) job->block_len); | ||
146 | |||
147 | job->statefn = rs_loadsig_s_weak; | ||
148 | |||
149 | return RS_RUNNING; | ||
150 | } | ||
151 | |||
152 | |||
153 | static rs_result rs_loadsig_s_blocklen(rs_job_t *job) | ||
154 | { | ||
155 | int l; | ||
156 | rs_result result; | ||
157 | |||
158 | if ((result = rs_suck_n4(job, &l)) != RS_DONE) | ||
159 | return result; | ||
160 | job->block_len = l; | ||
161 | |||
162 | if (job->block_len < 1) { | ||
163 | rs_error("block length of %d is bogus", (int) job->block_len); | ||
164 | return RS_CORRUPT; | ||
165 | } | ||
166 | |||
167 | job->statefn = rs_loadsig_s_stronglen; | ||
168 | job->stats.block_len = job->block_len; | ||
169 | |||
170 | return RS_RUNNING; | ||
171 | } | ||
172 | |||
173 | |||
174 | static rs_result rs_loadsig_s_magic(rs_job_t *job) | ||
175 | { | ||
176 | int l; | ||
177 | rs_result result; | ||
178 | |||
179 | if ((result = rs_suck_n4(job, &l)) != RS_DONE) { | ||
180 | return result; | ||
181 | } else if (l != RS_SIG_MAGIC) { | ||
182 | rs_error("wrong magic number %#10x for signature", l); | ||
183 | return RS_BAD_MAGIC; | ||
184 | } else { | ||
185 | rs_trace("got signature magic %#10x", l); | ||
186 | } | ||
187 | |||
188 | job->statefn = rs_loadsig_s_blocklen; | ||
189 | |||
190 | return RS_RUNNING; | ||
191 | } | ||
192 | |||
193 | |||
194 | /** | ||
195 | * \brief Read a signature from a file into an ::rs_signature_t structure | ||
196 | * in memory. | ||
197 | * | ||
198 | * Once there, it can be used to generate a delta to a newer version of | ||
199 | * the file. | ||
200 | * | ||
201 | * \note After loading the signatures, you must call | ||
202 | * rs_build_hash_table() before you can use them. | ||
203 | */ | ||
204 | rs_job_t *rs_loadsig_begin(rs_signature_t **signature) | ||
205 | { | ||
206 | rs_job_t *job; | ||
207 | |||
208 | job = rs_job_new("loadsig", rs_loadsig_s_magic); | ||
209 | *signature = job->signature = rs_alloc_struct(rs_signature_t); | ||
210 | job->signature->count = 0; | ||
211 | |||
212 | return job; | ||
213 | } | ||
214 | |||