librsync  2.0.1
sumset.h
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 #include <assert.h>
24 #include "hashtable.h"
25 #include "checksum.h"
26 
27 /** Signature of a single block. */
28 typedef struct rs_block_sig {
29  rs_weak_sum_t weak_sum; /**< Block's weak checksum. */
30  rs_strong_sum_t strong_sum; /**< Block's strong checksum. */
32 
33 void rs_block_sig_init(rs_block_sig_t *sig, rs_weak_sum_t weak_sum, rs_strong_sum_t *strong_sum, int strong_len);
34 
35 /** Signature of a whole file.
36  *
37  * This includes the all the block sums generated for a file and
38  * datastructures for fast matching against them. */
39 struct rs_signature {
40  int magic; /**< The signature magic value. */
41  int block_len; /**< The block length. */
42  int strong_sum_len; /**< The block strong sum length. */
43  int count; /**< Total number of blocks. */
44  int size; /**< Total number of blocks allocated. */
45  void *block_sigs; /**< The packed block_sigs for all blocks. */
46  hashtable_t *hashtable; /**< The hashtable for finding matches. */
47  /* The is extra stats not included in the hashtable stats. */
48 #ifndef HASHTABLE_NSTATS
49  long calc_strong_count; /**< The count of strongsum calcs done. */
50 #endif
51 };
52 
53 /** Initialize an rs_signature instance.
54  *
55  * \param *sig the signature to initialize.
56  *
57  * \param magic the signature magic value. Must be set to a valid magic value.
58  *
59  * \param block_len the block size to use. Must be > 0.
60  *
61  * \param strong_len the strongsum size to use. Must be <= the max strongsum
62  * size for the strongsum type indicated by the magic value. Use 0 to use the
63  * recommended size for the provided magic value.
64  *
65  * \param sig_fsize signature file size to preallocate required storage for.
66  * Use 0 if size is unknown. */
67 rs_result rs_signature_init(rs_signature_t *sig, int magic, int block_len, int strong_len, rs_long_t sig_fsize);
68 
69 /** Destroy an rs_signature instance. */
70 void rs_signature_done(rs_signature_t *sig);
71 
72 /** Add a block to an rs_signature instance. */
73 rs_block_sig_t *rs_signature_add_block(rs_signature_t *sig, rs_weak_sum_t weak_sum, rs_strong_sum_t *strong_sum);
74 
75 /** Find a matching block offset in a signature. */
76 rs_long_t rs_signature_find_match(rs_signature_t *sig, rs_weak_sum_t weak_sum, void const *buf, size_t len);
77 
78 /** Log the rs_signature_find_match() stats. */
79 void rs_signature_log_stats(rs_signature_t const *sig);
80 
81 /** Assert that a signature is valid.
82  *
83  * We don't use a static inline function here so that assert failure output
84  * points at where rs_signature_check() was called from. */
85 #define rs_signature_check(sig) do {\
86  assert(((sig)->magic == RS_BLAKE2_SIG_MAGIC && (sig)->strong_sum_len <= RS_BLAKE2_SUM_LENGTH)\
87  || ((sig)->magic == RS_MD4_SIG_MAGIC && (sig)->strong_sum_len <= RS_MD4_SUM_LENGTH));\
88  assert(0 < (sig)->block_len);\
89  assert(0 < (sig)->strong_sum_len && (sig)->strong_sum_len <= RS_MAX_STRONG_SUM_LENGTH);\
90  assert(0 <= (sig)->count && (sig)->count <= (sig)->size);\
91  assert(!(sig)->hashtable || (sig)->hashtable->count == (sig)->count);\
92 } while (0)
93 
94 /** Calculate the strong sum of a buffer. */
95 static inline void rs_signature_calc_strong_sum(rs_signature_t const *sig, void const *buf, size_t len,
96  rs_strong_sum_t *sum)
97 {
98  if (sig->magic == RS_BLAKE2_SIG_MAGIC) {
99  rs_calc_blake2_sum(buf, len, sum);
100  } else {
101  rs_calc_md4_sum(buf, len, sum);
102  }
103 }
int size
Total number of blocks allocated.
Definition: sumset.h:44
hashtable_t * hashtable
The hashtable for finding matches.
Definition: sumset.h:46
int count
Total number of blocks.
Definition: sumset.h:43
int block_len
The block length.
Definition: sumset.h:41
A signature file using the BLAKE2 hash.
Definition: librsync.h:107
long long rs_long_t
A long integer type that can handle the largest file offsets.
The hashtable type.
Definition: hashtable.h:123
rs_weak_sum_t weak_sum
Block&#39;s weak checksum.
Definition: sumset.h:29
int strong_sum_len
The block strong sum length.
Definition: sumset.h:42
void * block_sigs
The packed block_sigs for all blocks.
Definition: sumset.h:45
Signature of a whole file.
Definition: sumset.h:39
rs_result
Return codes from nonblocking rsync operations.
Definition: librsync.h:193
int magic
The signature magic value.
Definition: sumset.h:40
long calc_strong_count
The count of strongsum calcs done.
Definition: sumset.h:49
Signature of a single block.
Definition: sumset.h:28
rs_strong_sum_t strong_sum
Block&#39;s strong checksum.
Definition: sumset.h:30