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