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