librsync  2.3.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
24 * Load signatures from a file. */
25
26#include "config.h"
27#include <assert.h>
28#include <stdlib.h>
29#include "librsync.h"
30#include "job.h"
31#include "sumset.h"
32#include "stream.h"
33#include "netint.h"
34#include "trace.h"
35#include "util.h"
36
37static rs_result rs_loadsig_s_weak(rs_job_t *job);
38static rs_result rs_loadsig_s_strong(rs_job_t *job);
39
40/** Add a just-read-in checksum pair to the signature block. */
41static rs_result rs_loadsig_add_sum(rs_job_t *job, rs_strong_sum_t *strong)
42{
43 rs_signature_t *sig = job->signature;
44
45 if (rs_trace_enabled()) {
46 char hexbuf[RS_MAX_STRONG_SUM_LENGTH * 2 + 2];
47 rs_hexify(hexbuf, strong, sig->strong_sum_len);
48 rs_trace("got block: weak=" FMT_WEAKSUM ", strong=%s", job->weak_sig,
49 hexbuf);
50 }
51 rs_signature_add_block(job->signature, job->weak_sig, strong);
52 job->stats.sig_blocks++;
53 return RS_RUNNING;
54}
55
56static rs_result rs_loadsig_s_weak(rs_job_t *job)
57{
58 int l;
59 rs_result result;
60
61 if ((result = rs_suck_n4(job, &l)) != RS_DONE) {
62 if (result == RS_INPUT_ENDED) /* ending here is OK */
63 return RS_DONE;
64 return result;
65 }
66 job->weak_sig = l;
67 job->statefn = rs_loadsig_s_strong;
68 return RS_RUNNING;
69}
70
71static rs_result rs_loadsig_s_strong(rs_job_t *job)
72{
73 rs_result result;
74 rs_strong_sum_t *strongsum;
75
76 if ((result =
78 (void **)&strongsum)) != RS_DONE)
79 return result;
80 job->statefn = rs_loadsig_s_weak;
81 return rs_loadsig_add_sum(job, strongsum);
82}
83
84static rs_result rs_loadsig_s_stronglen(rs_job_t *job)
85{
86 int l;
87 rs_result result;
88
89 if ((result = rs_suck_n4(job, &l)) != RS_DONE)
90 return result;
91 if (l < 0 || l > RS_MAX_STRONG_SUM_LENGTH) {
92 rs_error("strong sum length %d is implausible", l);
93 return RS_CORRUPT;
94 }
95 rs_trace("got strong sum length %d", l);
96 job->sig_strong_len = l;
97 /* Initialize the signature. */
98 if ((result =
99 rs_signature_init(job->signature, job->sig_magic, job->sig_block_len,
100 job->sig_strong_len, job->sig_fsize)) != RS_DONE)
101 return result;
102 job->statefn = rs_loadsig_s_weak;
103 return RS_RUNNING;
104}
105
106static rs_result rs_loadsig_s_blocklen(rs_job_t *job)
107{
108 int l;
109 rs_result result;
110
111 if ((result = rs_suck_n4(job, &l)) != RS_DONE)
112 return result;
113 if (l < 1) {
114 rs_error("block length of %d is bogus", l);
115 return RS_CORRUPT;
116 }
117 rs_trace("got block length %d", l);
118 job->sig_block_len = l;
119 job->stats.block_len = l;
120 job->statefn = rs_loadsig_s_stronglen;
121 return RS_RUNNING;
122}
123
124static rs_result rs_loadsig_s_magic(rs_job_t *job)
125{
126 int l;
127 rs_result result;
128
129 if ((result = rs_suck_n4(job, &l)) != RS_DONE)
130 return result;
131 rs_trace("got signature magic %#x", l);
132 job->sig_magic = l;
133 job->statefn = rs_loadsig_s_blocklen;
134 return RS_RUNNING;
135}
136
138{
139 rs_job_t *job;
140
141 job = rs_job_new("loadsig", rs_loadsig_s_magic);
142 *signature = job->signature = rs_alloc_struct(rs_signature_t);
143 return job;
144}
Public header for librsync.
rs_result
Return codes from nonblocking rsync operations.
Definition: librsync.h:180
@ RS_RUNNING
The job is still running, and not yet finished or blocked.
Definition: librsync.h:183
@ RS_DONE
Completed successfully.
Definition: librsync.h:181
@ RS_CORRUPT
Unbelievable value in stream.
Definition: librsync.h:198
@ RS_INPUT_ENDED
Unexpected end of input file, perhaps due to a truncated file or dropped network connection.
Definition: librsync.h:190
LIBRSYNC_EXPORT 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:23
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:137
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:41
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:192
Manage librsync streams of IO.
The contents of this structure are private.
Definition: job.h:26
rs_weak_sum_t weak_sig
The weak signature digest used by readsums.c.
Definition: job.h:60
rs_result(* statefn)(rs_job_t *)
Callback for each processing step.
Definition: job.h:35
rs_long_t sig_fsize
The size of the signature file if available.
Definition: job.h:48
rs_signature_t * signature
Pointer to the signature that's being used by the operation.
Definition: job.h:51
rs_stats_t stats
Encoding statistics.
Definition: job.h:72
Signature of a whole file.
Definition: sumset.h:37
int strong_sum_len
The block strong sum length.
Definition: sumset.h:40
rs_long_t sig_blocks
Number of blocks described by the signature.
Definition: librsync.h:222
logging functions.
#define rs_trace_enabled()
Call this before putting too much effort into generating trace messages.
Definition: trace.h:69