librsync  2.3.4
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" /* IWYU pragma: keep */
27#include "librsync.h"
28#include "job.h"
29#include "sumset.h"
30#include "scoop.h"
31#include "netint.h"
32#include "trace.h"
33#include "util.h"
34
35static rs_result rs_loadsig_s_weak(rs_job_t *job);
36static rs_result rs_loadsig_s_strong(rs_job_t *job);
37
38/** Add a just-read-in checksum pair to the signature block. */
39static rs_result rs_loadsig_add_sum(rs_job_t *job, rs_strong_sum_t *strong)
40{
41 rs_signature_t *sig = job->signature;
42
43 if (rs_trace_enabled()) {
44 char hexbuf[RS_MAX_STRONG_SUM_LENGTH * 2 + 2];
45 rs_hexify(hexbuf, strong, sig->strong_sum_len);
46 rs_trace("got block: weak=" FMT_WEAKSUM ", strong=%s", job->weak_sig,
47 hexbuf);
48 }
49 rs_signature_add_block(job->signature, job->weak_sig, strong);
50 job->stats.sig_blocks++;
51 return RS_RUNNING;
52}
53
54static rs_result rs_loadsig_s_weak(rs_job_t *job)
55{
56 int l;
57 rs_result result;
58
59 if ((result = rs_suck_n4(job, &l)) != RS_DONE) {
60 if (result == RS_INPUT_ENDED) /* ending here is OK */
61 return RS_DONE;
62 return result;
63 }
64 job->weak_sig = l;
65 job->statefn = rs_loadsig_s_strong;
66 return RS_RUNNING;
67}
68
69static rs_result rs_loadsig_s_strong(rs_job_t *job)
70{
71 rs_result result;
72 rs_strong_sum_t *strongsum;
73
74 if ((result =
76 (void **)&strongsum)) != RS_DONE)
77 return result;
78 job->statefn = rs_loadsig_s_weak;
79 return rs_loadsig_add_sum(job, strongsum);
80}
81
82static rs_result rs_loadsig_s_stronglen(rs_job_t *job)
83{
84 int l;
85 rs_result result;
86
87 if ((result = rs_suck_n4(job, &l)) != RS_DONE)
88 return result;
89 if (l < 0 || l > RS_MAX_STRONG_SUM_LENGTH) {
90 rs_error("strong sum length %d is implausible", l);
91 return RS_CORRUPT;
92 }
93 rs_trace("got strong sum length %d", l);
94 job->sig_strong_len = l;
95 /* Initialize the signature. */
96 if ((result =
97 rs_signature_init(job->signature, job->sig_magic, job->sig_block_len,
98 job->sig_strong_len, job->sig_fsize)) != RS_DONE)
99 return result;
100 job->statefn = rs_loadsig_s_weak;
101 return RS_RUNNING;
102}
103
104static rs_result rs_loadsig_s_blocklen(rs_job_t *job)
105{
106 int l;
107 rs_result result;
108
109 if ((result = rs_suck_n4(job, &l)) != RS_DONE)
110 return result;
111 if (l < 1) {
112 rs_error("block length of %d is bogus", l);
113 return RS_CORRUPT;
114 }
115 rs_trace("got block length %d", l);
116 job->sig_block_len = l;
117 job->stats.block_len = l;
118 job->statefn = rs_loadsig_s_stronglen;
119 return RS_RUNNING;
120}
121
122static rs_result rs_loadsig_s_magic(rs_job_t *job)
123{
124 int l;
125 rs_result result;
126
127 if ((result = rs_suck_n4(job, &l)) != RS_DONE)
128 return result;
129 rs_trace("got signature magic %#x", l);
130 job->sig_magic = l;
131 job->statefn = rs_loadsig_s_blocklen;
132 return RS_RUNNING;
133}
134
136{
137 rs_job_t *job;
138
139 job = rs_job_new("loadsig", rs_loadsig_s_magic);
140 *signature = job->signature = rs_alloc_struct(rs_signature_t);
141 return job;
142}
Generic state-machine interface.
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
Network-byte-order output to the tube.
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:135
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:39
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:47
rs_weak_sum_t weak_sig
The weak signature digest used by readsums.c.
Definition: job.h:81
rs_result(* statefn)(rs_job_t *)
Callback for each processing step.
Definition: job.h:56
rs_long_t sig_fsize
The size of the signature file if available.
Definition: job.h:69
rs_signature_t * signature
Pointer to the signature that's being used by the operation.
Definition: job.h:72
rs_stats_t stats
Encoding statistics.
Definition: job.h:93
Signature of a whole file.
Definition: sumset.h:44
int strong_sum_len
The block strong sum length.
Definition: sumset.h:47
rs_long_t sig_blocks
Number of blocks described by the signature.
Definition: librsync.h:222
The rs_signature class implementation of a file signature.
rs_result rs_signature_init(rs_signature_t *sig, rs_magic_number magic, size_t block_len, size_t strong_len, rs_long_t sig_fsize)
Initialize an rs_signature instance.
Definition: sumset.c:183
rs_block_sig_t * rs_signature_add_block(rs_signature_t *sig, rs_weak_sum_t weak_sum, rs_strong_sum_t *strong_sum)
Add a block to an rs_signature instance.
Definition: sumset.c:222
logging functions.
#define rs_trace_enabled()
Call this before putting too much effort into generating trace messages.
Definition: trace.h:70
Misc utility functions used by librsync.
#define rs_alloc_struct(type)
Allocate and zero-fill an instance of TYPE.
Definition: util.h:41