librsync  2.0.2
readsums.c
Go to the documentation of this file.
1 /*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*-
2  *
3  * librsync -- the library for network deltas
4  *
5  * Copyright (C) 1999, 2000, 2001 by Martin Pool <mbp@sourcefrog.net>
6  * Copyright (C) 1999 by Andrew Tridgell <tridge@samba.org>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22 
23 /** \file readsums.c Load signatures from a file. */
24 
25 #include "config.h"
26 
27 #include <assert.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 
32 #include "librsync.h"
33 #include "sumset.h"
34 #include "job.h"
35 #include "trace.h"
36 #include "netint.h"
37 #include "util.h"
38 #include "stream.h"
39 
40 static rs_result rs_loadsig_s_weak(rs_job_t *job);
41 static rs_result rs_loadsig_s_strong(rs_job_t *job);
42 
43 /** Add a just-read-in checksum pair to the signature block. */
44 static rs_result rs_loadsig_add_sum(rs_job_t *job, rs_strong_sum_t *strong)
45 {
46  rs_signature_t *sig = job->signature;
47 
48  if (rs_trace_enabled()) {
49  char hexbuf[RS_MAX_STRONG_SUM_LENGTH * 2 + 2];
50  rs_hexify(hexbuf, strong, sig->strong_sum_len);
51  rs_trace("got block: weak=" FMT_WEAKSUM ", strong=%s", job->weak_sig,
52  hexbuf);
53  }
54  rs_signature_add_block(job->signature, job->weak_sig, strong);
55  job->stats.sig_blocks++;
56  return RS_RUNNING;
57 }
58 
59 static rs_result rs_loadsig_s_weak(rs_job_t *job)
60 {
61  int l;
62  rs_result result;
63 
64  if ((result = rs_suck_n4(job, &l)) != RS_DONE) {
65  if (result == RS_INPUT_ENDED) /* ending here is OK */
66  return RS_DONE;
67  return result;
68  }
69  job->weak_sig = l;
70  job->statefn = rs_loadsig_s_strong;
71  return RS_RUNNING;
72 }
73 
74 static rs_result rs_loadsig_s_strong(rs_job_t *job)
75 {
76  rs_result result;
77  rs_strong_sum_t *strongsum;
78 
79  if ((result =
81  (void **)&strongsum)) != RS_DONE)
82  return result;
83  job->statefn = rs_loadsig_s_weak;
84  return rs_loadsig_add_sum(job, strongsum);
85 }
86 
87 static rs_result rs_loadsig_s_stronglen(rs_job_t *job)
88 {
89  int l;
90  rs_result result;
91 
92  if ((result = rs_suck_n4(job, &l)) != RS_DONE)
93  return result;
94  if (l < 0 || l > RS_MAX_STRONG_SUM_LENGTH) {
95  rs_error("strong sum length %d is implausible", l);
96  return RS_CORRUPT;
97  }
98  rs_trace("got strong sum length %d", l);
99  job->sig_strong_len = l;
100  /* Initialize the signature. */
101  if ((result =
102  rs_signature_init(job->signature, job->sig_magic, job->sig_block_len,
103  job->sig_strong_len, job->sig_fsize)) != RS_DONE)
104  return result;
105  job->statefn = rs_loadsig_s_weak;
106  return RS_RUNNING;
107 }
108 
109 static rs_result rs_loadsig_s_blocklen(rs_job_t *job)
110 {
111  int l;
112  rs_result result;
113 
114  if ((result = rs_suck_n4(job, &l)) != RS_DONE)
115  return result;
116  if (l < 1) {
117  rs_error("block length of %d is bogus", l);
118  return RS_CORRUPT;
119  }
120  rs_trace("got block length %d", l);
121  job->sig_block_len = l;
122  job->stats.block_len = l;
123  job->statefn = rs_loadsig_s_stronglen;
124  return RS_RUNNING;
125 }
126 
127 static rs_result rs_loadsig_s_magic(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  rs_trace("got signature magic %#x", l);
135  job->sig_magic = l;
136  job->statefn = rs_loadsig_s_blocklen;
137  return RS_RUNNING;
138 }
139 
141 {
142  rs_job_t *job;
143 
144  job = rs_job_new("loadsig", rs_loadsig_s_magic);
145  *signature = job->signature = rs_alloc_struct(rs_signature_t);
146  return job;
147 }
rs_job::weak_sig
rs_weak_sum_t weak_sig
The weak signature digest used by readsums.c.
Definition: job.h:60
rs_signature::strong_sum_len
int strong_sum_len
The block strong sum length.
Definition: sumset.h:40
rs_result
rs_result
Definition: librsync.h:161
rs_loadsig_add_sum
static rs_result rs_loadsig_add_sum(rs_job_t *job, rs_strong_sum_t *strong)
Add a just-read-in checksum pair to the signature block.
Definition: readsums.c:44
rs_job::signature
rs_signature_t * signature
Pointer to the signature that's being used by the operation.
Definition: job.h:51
trace.h
RS_DONE
@ RS_DONE
Completed successfully.
Definition: librsync.h:162
rs_stats::sig_blocks
rs_long_t sig_blocks
Number of blocks described by the signature.
Definition: librsync.h:203
rs_hexify
void rs_hexify(char *to_buf, void const *from_buf, int from_len)
Convert from_len bytes at from_buf into a hex representation in to_buf, which must be twice as long p...
Definition: hex.c:32
rs_job::statefn
rs_result(* statefn)(rs_job_t *)
Callback for each processing step.
Definition: job.h:35
librsync.h
rs_job::sig_fsize
rs_long_t sig_fsize
The size of the signature file if available.
Definition: job.h:48
rs_job
Definition: job.h:26
RS_RUNNING
@ RS_RUNNING
The job is still running, and not yet finished or blocked.
Definition: librsync.h:164
RS_INPUT_ENDED
@ RS_INPUT_ENDED
Unexpected end of input file, perhaps due to a truncated file or dropped network connection.
Definition: librsync.h:171
rs_job::stats
rs_stats_t stats
Encoding statistics.
Definition: job.h:72
RS_CORRUPT
@ RS_CORRUPT
Unbelievable value in stream.
Definition: librsync.h:179
rs_signature
Signature of a whole file.
Definition: sumset.h:37
rs_loadsig_begin
rs_job_t * rs_loadsig_begin(rs_signature_t **signature)
Read a signature from a file into an rs_signature structure in memory.
Definition: readsums.c:140
rs_scoop_read
rs_result rs_scoop_read(rs_job_t *job, size_t len, void **ptr)
Read LEN bytes if possible, and remove them from the input scoop.
Definition: scoop.c:190