librsync  2.3.4
sumset.h
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 sumset.h
24  * The rs_signature class implementation of a file signature. */
25 #ifndef SUMSET_H
26 # define SUMSET_H
27 
28 # include <assert.h>
29 # include <stddef.h>
30 # include "hashtable.h"
31 # include "checksum.h"
32 # include "librsync.h"
33 
34 /** Signature of a single block. */
35 typedef struct rs_block_sig {
36  rs_weak_sum_t weak_sum; /**< Block's weak checksum. */
37  rs_strong_sum_t strong_sum; /**< Block's strong checksum. */
39 
40 /** Signature of a whole file.
41  *
42  * This includes the all the block sums generated for a file and datastructures
43  * for fast matching against them. */
44 struct rs_signature {
45  int magic; /**< The signature magic value. */
46  int block_len; /**< The block length. */
47  int strong_sum_len; /**< The block strong sum length. */
48  int count; /**< Total number of blocks. */
49  int size; /**< Total number of blocks allocated. */
50  void *block_sigs; /**< The packed block_sigs for all blocks. */
51  hashtable_t *hashtable; /**< The hashtable for finding matches. */
52  /* The is extra stats not included in the hashtable stats. */
53 # ifndef HASHTABLE_NSTATS
54  long calc_strong_count; /**< The count of strongsum calcs done. */
55 # endif
56 };
57 
58 /** Initialize an rs_signature instance.
59  *
60  * \param *sig the signature to initialize.
61  *
62  * \param magic - the magic type to use (0 for "recommended").
63  *
64  * \param block_len - the block length to use (0 for "recommended").
65  *
66  * \param strong_len - the strongsum length to use (0 for "maximum", -1 for
67  * "minimum"). Must be <= the max strongsum size for the strongsum type
68  * indicated by the magic value.
69  *
70  * \param sig_fsize - the signature file size (-1 for "unknown"). Used to
71  * preallocate required storage. */
73  size_t block_len, size_t strong_len,
74  rs_long_t sig_fsize);
75 
76 /** Destroy an rs_signature instance. */
78 
79 /** Add a block to an rs_signature instance. */
81  rs_weak_sum_t weak_sum,
82  rs_strong_sum_t *strong_sum);
83 
84 /** Find a matching block offset in a signature. */
85 rs_long_t rs_signature_find_match(rs_signature_t *sig, rs_weak_sum_t weak_sum,
86  void const *buf, size_t len);
87 
88 /** Assert that rs_sig_args() args for rs_signature_init() are valid.
89  *
90  * We don't use a static inline function here so that assert failure output
91  * points at where rs_sig_args_check() was called from. */
92 # define rs_sig_args_check(magic, block_len, strong_len) do {\
93  assert(((magic) & ~0xff) == (RS_MD4_SIG_MAGIC & ~0xff));\
94  assert(((magic) & 0xf0) == 0x30 || ((magic) & 0xf0) == 0x40);\
95  assert((((magic) & 0x0f) == 0x06 &&\
96  (int)(strong_len) <= RS_MD4_SUM_LENGTH) ||\
97  (((magic) & 0x0f) == 0x07 &&\
98  (int)(strong_len) <= RS_BLAKE2_SUM_LENGTH));\
99  assert(0 < (block_len));\
100  assert(0 < (strong_len) && (strong_len) <= RS_MAX_STRONG_SUM_LENGTH);\
101 } while (0)
102 
103 /** Assert that a signature is valid.
104  *
105  * We don't use a static inline function here so that assert failure output
106  * points at where rs_signature_check() was called from. */
107 # define rs_signature_check(sig) do {\
108  rs_sig_args_check((sig)->magic, (sig)->block_len, (sig)->strong_sum_len);\
109  assert(0 <= (sig)->count && (sig)->count <= (sig)->size);\
110  assert(!(sig)->hashtable || (sig)->hashtable->count <= (sig)->count);\
111 } while (0)
112 
113 /** Get the weaksum kind for a signature. */
115  *sig)
116 {
117  return (sig->magic & 0xf0) == 0x30 ? RS_ROLLSUM : RS_RABINKARP;
118 }
119 
120 /** Get the strongsum kind for a signature. */
122  *sig)
123 {
124  return (sig->magic & 0x0f) == 0x06 ? RS_MD4 : RS_BLAKE2;
125 }
126 
127 /** Calculate the weak sum of a buffer. */
128 static inline rs_weak_sum_t rs_signature_calc_weak_sum(rs_signature_t const
129  *sig, void const *buf,
130  size_t len)
131 {
132  return rs_calc_weak_sum(rs_signature_weaksum_kind(sig), buf, len);
133 }
134 
135 /** Calculate the strong sum of a buffer. */
136 static inline void rs_signature_calc_strong_sum(rs_signature_t const *sig,
137  void const *buf, size_t len,
138  rs_strong_sum_t *sum)
139 {
140  rs_calc_strong_sum(rs_signature_strongsum_kind(sig), buf, len, sum);
141 }
142 
143 #endif /* !SUMSET_H */
int size
Total number of blocks allocated.
Definition: sumset.h:49
hashtable_t * hashtable
The hashtable for finding matches.
Definition: sumset.h:51
int count
Total number of blocks.
Definition: sumset.h:48
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
int block_len
The block length.
Definition: sumset.h:46
void rs_calc_strong_sum(strongsum_kind_t kind, void const *buf, size_t len, rs_strong_sum_t *sum)
Calculate a strongsum.
Definition: checksum.c:59
rs_weak_sum_t rs_calc_weak_sum(weaksum_kind_t kind, void const *buf, size_t len)
Calculate a weaksum.
Definition: checksum.c:34
struct rs_block_sig rs_block_sig_t
Signature of a single block.
static void rs_signature_calc_strong_sum(rs_signature_t const *sig, void const *buf, size_t len, rs_strong_sum_t *sum)
Calculate the strong sum of a buffer.
Definition: sumset.h:136
rs_weak_sum_t weak_sum
Block&#39;s weak checksum.
Definition: sumset.h:36
int strong_sum_len
The block strong sum length.
Definition: sumset.h:47
void * block_sigs
The packed block_sigs for all blocks.
Definition: sumset.h:50
Public header for librsync.
Signature of a whole file.
Definition: sumset.h:44
void rs_signature_done(rs_signature_t *sig)
Destroy an rs_signature instance.
Definition: sumset.c:215
static rs_weak_sum_t rs_signature_calc_weak_sum(rs_signature_t const *sig, void const *buf, size_t len)
Calculate the weak sum of a buffer.
Definition: sumset.h:128
strongsum_kind_t
Strongsum implementations.
Definition: checksum.h:41
rs_result
Return codes from nonblocking rsync operations.
Definition: librsync.h:180
weaksum_kind_t
Weaksum implementations.
Definition: checksum.h:35
int magic
The signature magic value.
Definition: sumset.h:45
rs_long_t rs_signature_find_match(rs_signature_t *sig, rs_weak_sum_t weak_sum, void const *buf, size_t len)
Find a matching block offset in a signature.
Definition: sumset.c:242
Abstract wrappers around different weaksum and strongsum implementations.
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
long calc_strong_count
The count of strongsum calcs done.
Definition: sumset.h:54
Signature of a single block.
Definition: sumset.h:35
rs_magic_number
A uint32 magic number, emitted in bigendian/network order at the start of librsync files...
Definition: librsync.h:65
The hashtable type.
Definition: hashtable.h:128
static strongsum_kind_t rs_signature_strongsum_kind(rs_signature_t const *sig)
Get the strongsum kind for a signature.
Definition: sumset.h:121
A generic open addressing hashtable.
rs_strong_sum_t strong_sum
Block&#39;s strong checksum.
Definition: sumset.h:37
static weaksum_kind_t rs_signature_weaksum_kind(rs_signature_t const *sig)
Get the weaksum kind for a signature.
Definition: sumset.h:114