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. */
31 } rs_block_sig_t;
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 magic type to use (0 for "recommended").
56  *
57  * \param block_len - the block length to use (0 for "recommended").
58  *
59  * \param strong_len - the strongsum length to use (0 for "maximum", -1 for
60  * "minimum"). Must be <= the max strongsum size for the strongsum type
61  * indicated by the magic value.
62  *
63  * \param sig_fsize - the signature file size (-1 for "unknown"). Used to
64  * preallocate required storage. */
65 rs_result rs_signature_init(rs_signature_t *sig, rs_magic_number magic,
66                             size_t block_len, size_t strong_len,
67                             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,
74                                        rs_weak_sum_t weak_sum,
75                                        rs_strong_sum_t *strong_sum);
76 
77 /** Find a matching block offset in a signature. */
78 rs_long_t rs_signature_find_match(rs_signature_t *sig, rs_weak_sum_t weak_sum,
79                                   void const *buf, size_t len);
80 
81 /** Assert that rs_sig_args() args for rs_signature_init() are valid.
82  *
83  * We don't use a static inline function here so that assert failure output
84  * points at where rs_sig_args_check() was called from. */
85 #define rs_sig_args_check(magic, block_len, strong_len) do {\
86     assert(((magic) & ~0xff) == (RS_MD4_SIG_MAGIC & ~0xff));\
87     assert(((magic) & 0xf0) == 0x30 || ((magic) & 0xf0) == 0x40);\
88     assert((((magic) & 0x0f) == 0x06 &&\
89 	    (int)(strong_len) <= RS_MD4_SUM_LENGTH) ||\
90 	   (((magic) & 0x0f) == 0x07 &&\
91 	    (int)(strong_len) <= RS_BLAKE2_SUM_LENGTH));\
92     assert(0 < (block_len));\
93     assert(0 < (strong_len) && (strong_len) <= RS_MAX_STRONG_SUM_LENGTH);\
94 } while (0)
95 
96 /** Assert that a signature is valid.
97  *
98  * We don't use a static inline function here so that assert failure output
99  * points at where rs_signature_check() was called from. */
100 #define rs_signature_check(sig) do {\
101     rs_sig_args_check((sig)->magic, (sig)->block_len, (sig)->strong_sum_len);\
102     assert(0 <= (sig)->count && (sig)->count <= (sig)->size);\
103     assert(!(sig)->hashtable || (sig)->hashtable->count <= (sig)->count);\
104 } while (0)
105 
106 /** Get the weaksum kind for a signature. */
rs_signature_weaksum_kind(rs_signature_t const * sig)107 static inline weaksum_kind_t rs_signature_weaksum_kind(rs_signature_t const
108                                                        *sig)
109 {
110     return (sig->magic & 0xf0) == 0x30 ? RS_ROLLSUM : RS_RABINKARP;
111 }
112 
113 /** Get the strongsum kind for a signature. */
rs_signature_strongsum_kind(rs_signature_t const * sig)114 static inline strongsum_kind_t rs_signature_strongsum_kind(rs_signature_t const
115                                                            *sig)
116 {
117     return (sig->magic & 0x0f) == 0x06 ? RS_MD4 : RS_BLAKE2;
118 }
119 
120 /** Calculate the weak sum of a buffer. */
rs_signature_calc_weak_sum(rs_signature_t const * sig,void const * buf,size_t len)121 static inline rs_weak_sum_t rs_signature_calc_weak_sum(rs_signature_t const
122                                                        *sig, void const *buf,
123                                                        size_t len)
124 {
125     return rs_calc_weak_sum(rs_signature_weaksum_kind(sig), buf, len);
126 }
127 
128 /** Calculate the strong sum of a buffer. */
rs_signature_calc_strong_sum(rs_signature_t const * sig,void const * buf,size_t len,rs_strong_sum_t * sum)129 static inline void rs_signature_calc_strong_sum(rs_signature_t const *sig,
130                                                 void const *buf, size_t len,
131                                                 rs_strong_sum_t *sum)
132 {
133     rs_calc_strong_sum(rs_signature_strongsum_kind(sig), buf, len, sum);
134 }
135