\doxysubsection{sumset.\+c}
\label{sumset_8c_source}\index{sumset.c@{sumset.c}}

\begin{DoxyCode}{0}
\DoxyCodeLine{00001 \textcolor{comment}{/*= -\/*-\/ c-\/basic-\/offset: 4; indent-\/tabs-\/mode: nil; -\/*-\/}}
\DoxyCodeLine{00002 \textcolor{comment}{ *}}
\DoxyCodeLine{00003 \textcolor{comment}{ * librsync -\/-\/ library for network deltas}}
\DoxyCodeLine{00004 \textcolor{comment}{ *}}
\DoxyCodeLine{00005 \textcolor{comment}{ * Copyright (C) 1999, 2000, 2001 by Martin Pool <mbp@sourcefrog.net>}}
\DoxyCodeLine{00006 \textcolor{comment}{ * Copyright (C) 1999 by Andrew Tridgell}}
\DoxyCodeLine{00007 \textcolor{comment}{ *}}
\DoxyCodeLine{00008 \textcolor{comment}{ * This program is free software; you can redistribute it and/or modify}}
\DoxyCodeLine{00009 \textcolor{comment}{ * it under the terms of the GNU Lesser General Public License as published by}}
\DoxyCodeLine{00010 \textcolor{comment}{ * the Free Software Foundation; either version 2.1 of the License, or}}
\DoxyCodeLine{00011 \textcolor{comment}{ * (at your option) any later version.}}
\DoxyCodeLine{00012 \textcolor{comment}{ *}}
\DoxyCodeLine{00013 \textcolor{comment}{ * This program is distributed in the hope that it will be useful,}}
\DoxyCodeLine{00014 \textcolor{comment}{ * but WITHOUT ANY WARRANTY; without even the implied warranty of}}
\DoxyCodeLine{00015 \textcolor{comment}{ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the}}
\DoxyCodeLine{00016 \textcolor{comment}{ * GNU Lesser General Public License for more details.}}
\DoxyCodeLine{00017 \textcolor{comment}{ *}}
\DoxyCodeLine{00018 \textcolor{comment}{ * You should have received a copy of the GNU Lesser General Public License}}
\DoxyCodeLine{00019 \textcolor{comment}{ * along with this program; if not, write to the Free Software}}
\DoxyCodeLine{00020 \textcolor{comment}{ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.}}
\DoxyCodeLine{00021 \textcolor{comment}{ */}}
\DoxyCodeLine{00022 }
\DoxyCodeLine{00023 \textcolor{preprocessor}{\#include "{}config.h"{}}             \textcolor{comment}{/* IWYU pragma: keep */}}
\DoxyCodeLine{00024 \textcolor{preprocessor}{\#include <stdlib.h>}}
\DoxyCodeLine{00025 \textcolor{preprocessor}{\#include <string.h>}}
\DoxyCodeLine{00026 \textcolor{preprocessor}{\#include "{}librsync.h"{}}}
\DoxyCodeLine{00027 \textcolor{preprocessor}{\#include "{}sumset.h"{}}}
\DoxyCodeLine{00028 \textcolor{preprocessor}{\#include "{}trace.h"{}}}
\DoxyCodeLine{00029 \textcolor{preprocessor}{\#include "{}util.h"{}}}
\DoxyCodeLine{00030 }
\DoxyCodeLine{00031 \textcolor{keyword}{static} \textcolor{keywordtype}{void} rs\_block\_sig\_init(rs\_block\_sig\_t *sig, rs\_weak\_sum\_t weak\_sum,}
\DoxyCodeLine{00032                               rs\_strong\_sum\_t *strong\_sum, \textcolor{keywordtype}{int} strong\_len)}
\DoxyCodeLine{00033 \{}
\DoxyCodeLine{00034     sig-\/>weak\_sum = weak\_sum;}
\DoxyCodeLine{00035     \textcolor{keywordflow}{if} (strong\_sum)}
\DoxyCodeLine{00036         memcpy(sig-\/>strong\_sum, strong\_sum, (\textcolor{keywordtype}{size\_t})strong\_len);}
\DoxyCodeLine{00037 \}}
\DoxyCodeLine{00038 }
\DoxyCodeLine{00039 \textcolor{keyword}{static} \textcolor{keyword}{inline} \textcolor{keywordtype}{unsigned} rs\_block\_sig\_hash(\textcolor{keyword}{const} rs\_block\_sig\_t *sig)}
\DoxyCodeLine{00040 \{}
\DoxyCodeLine{00041     \textcolor{keywordflow}{return} (\textcolor{keywordtype}{unsigned})sig-\/>weak\_sum;}
\DoxyCodeLine{00042 \}}
\DoxyCodeLine{00043 }
\DoxyCodeLine{00044 \textcolor{keyword}{typedef} \textcolor{keyword}{struct }rs\_block\_match \{}
\DoxyCodeLine{00045     rs\_block\_sig\_t block\_sig;}
\DoxyCodeLine{00046     rs\_signature\_t *signature;}
\DoxyCodeLine{00047     \textcolor{keyword}{const} \textcolor{keywordtype}{void} *buf;}
\DoxyCodeLine{00048     \textcolor{keywordtype}{size\_t} len;}
\DoxyCodeLine{00049 \} rs\_block\_match\_t;}
\DoxyCodeLine{00050 }
\DoxyCodeLine{00051 \textcolor{keyword}{static} \textcolor{keywordtype}{void} rs\_block\_match\_init(rs\_block\_match\_t *match, rs\_signature\_t *sig,}
\DoxyCodeLine{00052                                 rs\_weak\_sum\_t weak\_sum,}
\DoxyCodeLine{00053                                 rs\_strong\_sum\_t *strong\_sum, \textcolor{keyword}{const} \textcolor{keywordtype}{void} *buf,}
\DoxyCodeLine{00054                                 \textcolor{keywordtype}{size\_t} len)}
\DoxyCodeLine{00055 \{}
\DoxyCodeLine{00056     rs\_block\_sig\_init(\&match-\/>block\_sig, weak\_sum, strong\_sum,}
\DoxyCodeLine{00057                       sig-\/>strong\_sum\_len);}
\DoxyCodeLine{00058     match-\/>signature = sig;}
\DoxyCodeLine{00059     match-\/>buf = buf;}
\DoxyCodeLine{00060     match-\/>len = len;}
\DoxyCodeLine{00061 \}}
\DoxyCodeLine{00062 }
\DoxyCodeLine{00063 \textcolor{keyword}{static} \textcolor{keyword}{inline} \textcolor{keywordtype}{int} rs\_block\_match\_cmp(rs\_block\_match\_t *match,}
\DoxyCodeLine{00064                                      \textcolor{keyword}{const} rs\_block\_sig\_t *block\_sig)}
\DoxyCodeLine{00065 \{}
\DoxyCodeLine{00066     \textcolor{comment}{/* If buf is not NULL, the strong sum is yet to be calculated. */}}
\DoxyCodeLine{00067     \textcolor{keywordflow}{if} (match-\/>buf) \{}
\DoxyCodeLine{00068 \textcolor{preprocessor}{\#ifndef HASHTABLE\_NSTATS}}
\DoxyCodeLine{00069         match-\/>signature-\/>calc\_strong\_count++;}
\DoxyCodeLine{00070 \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{00071         rs\_signature\_calc\_strong\_sum(match-\/>signature, match-\/>buf, match-\/>len,}
\DoxyCodeLine{00072                                      \&(match-\/>block\_sig.strong\_sum));}
\DoxyCodeLine{00073         match-\/>buf = NULL;}
\DoxyCodeLine{00074     \}}
\DoxyCodeLine{00075     \textcolor{keywordflow}{return} memcmp(\&match-\/>block\_sig.strong\_sum, \&block\_sig-\/>strong\_sum,}
\DoxyCodeLine{00076                   (\textcolor{keywordtype}{size\_t})match-\/>signature-\/>strong\_sum\_len);}
\DoxyCodeLine{00077 \}}
\DoxyCodeLine{00078 }
\DoxyCodeLine{00079 \textcolor{comment}{/* Disable mix32() in the hashtable because RabinKarp doesn't need it. We}}
\DoxyCodeLine{00080 \textcolor{comment}{   manually apply mix32() to rollsums before using them in the hashtable. */}}
\DoxyCodeLine{00081 \textcolor{preprocessor}{\#define HASHTABLE\_NMIX32}}
\DoxyCodeLine{00082 \textcolor{comment}{/* Instantiate hashtable for rs\_block\_sig and rs\_block\_match. */}}
\DoxyCodeLine{00083 \textcolor{preprocessor}{\#define ENTRY rs\_block\_sig}}
\DoxyCodeLine{00084 \textcolor{preprocessor}{\#define MATCH rs\_block\_match}}
\DoxyCodeLine{00085 \textcolor{preprocessor}{\#define NAME hashtable}}
\DoxyCodeLine{00086 \textcolor{preprocessor}{\#include "{}hashtable.h"{}}}
\DoxyCodeLine{00087 }
\DoxyCodeLine{00088 \textcolor{comment}{/* Get the size of a packed rs\_block\_sig\_t. */}}
\DoxyCodeLine{00089 \textcolor{keyword}{static} \textcolor{keyword}{inline} \textcolor{keywordtype}{size\_t} rs\_block\_sig\_size(\textcolor{keyword}{const} rs\_signature\_t *sig)}
\DoxyCodeLine{00090 \{}
\DoxyCodeLine{00091     \textcolor{comment}{/* Round up to multiple of sizeof(weak\_sum) to align memory correctly. */}}
\DoxyCodeLine{00092     \textcolor{keyword}{const} \textcolor{keywordtype}{size\_t} mask = \textcolor{keyword}{sizeof}(rs\_weak\_sum\_t)-\/ 1;}
\DoxyCodeLine{00093     \textcolor{keywordflow}{return} (offsetof(rs\_block\_sig\_t, strong\_sum) +}
\DoxyCodeLine{00094             (((size\_t)sig-\/>strong\_sum\_len + mask) \& \string~mask));}
\DoxyCodeLine{00095 \}}
\DoxyCodeLine{00096 }
\DoxyCodeLine{00097 \textcolor{comment}{/* Get the pointer to the block\_sig\_t from a block index. */}}
\DoxyCodeLine{00098 \textcolor{keyword}{static} \textcolor{keyword}{inline} rs\_block\_sig\_t *rs\_block\_sig\_ptr(\textcolor{keyword}{const} rs\_signature\_t *sig,}
\DoxyCodeLine{00099                                                \textcolor{keywordtype}{int} block\_idx)}
\DoxyCodeLine{00100 \{}
\DoxyCodeLine{00101     \textcolor{keywordflow}{return} (rs\_block\_sig\_t *)((\textcolor{keywordtype}{char} *)sig-\/>block\_sigs +}
\DoxyCodeLine{00102                                block\_idx * rs\_block\_sig\_size(sig));}
\DoxyCodeLine{00103 \}}
\DoxyCodeLine{00104 }
\DoxyCodeLine{00105 \textcolor{comment}{/* Get the index of a block from a block\_sig\_t pointer. */}}
\DoxyCodeLine{00106 \textcolor{keyword}{static} \textcolor{keyword}{inline} \textcolor{keywordtype}{int} rs\_block\_sig\_idx(\textcolor{keyword}{const} rs\_signature\_t *sig,}
\DoxyCodeLine{00107                                    rs\_block\_sig\_t *block\_sig)}
\DoxyCodeLine{00108 \{}
\DoxyCodeLine{00109     \textcolor{keywordflow}{return} (\textcolor{keywordtype}{int})(((\textcolor{keywordtype}{char} *)block\_sig -\/}
\DoxyCodeLine{00110                   (\textcolor{keywordtype}{char} *)sig-\/>block\_sigs) / rs\_block\_sig\_size(sig));}
\DoxyCodeLine{00111 \}}
\DoxyCodeLine{00112 }
\DoxyCodeLine{00113 rs\_result rs\_sig\_args(rs\_long\_t old\_fsize, rs\_magic\_number * magic,}
\DoxyCodeLine{00114                       \textcolor{keywordtype}{size\_t} *block\_len, \textcolor{keywordtype}{size\_t} *strong\_len)}
\DoxyCodeLine{00115 \{}
\DoxyCodeLine{00116     \textcolor{keywordtype}{size\_t} rec\_block\_len;       \textcolor{comment}{/* the recomended block\_len for the given}}
\DoxyCodeLine{00117 \textcolor{comment}{                                   old\_fsize. */}}
\DoxyCodeLine{00118     \textcolor{keywordtype}{size\_t} min\_strong\_len;      \textcolor{comment}{/* the minimum strong\_len for the given}}
\DoxyCodeLine{00119 \textcolor{comment}{                                   old\_fsize and block\_len. */}}
\DoxyCodeLine{00120     \textcolor{keywordtype}{size\_t} max\_strong\_len;      \textcolor{comment}{/* the maximum strong\_len for the given magic. */}}
\DoxyCodeLine{00121 }
\DoxyCodeLine{00122     \textcolor{comment}{/* Check and set default arguments. */}}
\DoxyCodeLine{00123     *magic = *magic ? *magic : RS\_RK\_BLAKE2\_SIG\_MAGIC;}
\DoxyCodeLine{00124     \textcolor{keywordflow}{switch} (*magic) \{}
\DoxyCodeLine{00125     \textcolor{keywordflow}{case} RS\_BLAKE2\_SIG\_MAGIC:}
\DoxyCodeLine{00126     \textcolor{keywordflow}{case} RS\_RK\_BLAKE2\_SIG\_MAGIC:}
\DoxyCodeLine{00127         max\_strong\_len = RS\_BLAKE2\_SUM\_LENGTH;}
\DoxyCodeLine{00128         \textcolor{keywordflow}{break};}
\DoxyCodeLine{00129     \textcolor{keywordflow}{case} RS\_MD4\_SIG\_MAGIC:}
\DoxyCodeLine{00130     \textcolor{keywordflow}{case} RS\_RK\_MD4\_SIG\_MAGIC:}
\DoxyCodeLine{00131         max\_strong\_len = RS\_MD4\_SUM\_LENGTH;}
\DoxyCodeLine{00132         \textcolor{keywordflow}{break};}
\DoxyCodeLine{00133     \textcolor{keywordflow}{default}:}
\DoxyCodeLine{00134         rs\_error(\textcolor{stringliteral}{"{}invalid magic \%\#x"{}}, *magic);}
\DoxyCodeLine{00135         \textcolor{keywordflow}{return} RS\_BAD\_MAGIC;}
\DoxyCodeLine{00136     \}}
\DoxyCodeLine{00137     \textcolor{comment}{/* The recommended block\_len is sqrt(old\_fsize) with a 256 min size rounded}}
\DoxyCodeLine{00138 \textcolor{comment}{       down to a multiple of the 128 byte blake2b blocksize to give a}}
\DoxyCodeLine{00139 \textcolor{comment}{       reasonable compromise between signature size, delta size, and}}
\DoxyCodeLine{00140 \textcolor{comment}{       performance. If the old\_fsize is unknown, we use the default. */}}
\DoxyCodeLine{00141     \textcolor{keywordflow}{if} (old\_fsize < 0) \{}
\DoxyCodeLine{00142         rec\_block\_len = RS\_DEFAULT\_BLOCK\_LEN;}
\DoxyCodeLine{00143     \} \textcolor{keywordflow}{else} \{}
\DoxyCodeLine{00144         rec\_block\_len =}
\DoxyCodeLine{00145             old\_fsize <= 256 * 256 ? 256 : rs\_long\_sqrt(old\_fsize) \& \string~127;}
\DoxyCodeLine{00146     \}}
\DoxyCodeLine{00147     \textcolor{keywordflow}{if} (*block\_len == 0)}
\DoxyCodeLine{00148         *block\_len = rec\_block\_len;}
\DoxyCodeLine{00149     \textcolor{comment}{/* The recommended strong\_len assumes the worst case new\_fsize = old\_fsize}}
\DoxyCodeLine{00150 \textcolor{comment}{       + 16MB with no matches. This results in comparing a block at every byte}}
\DoxyCodeLine{00151 \textcolor{comment}{       offset against all the blocks in the signature, or new\_fsize*block\_num}}
\DoxyCodeLine{00152 \textcolor{comment}{       comparisons. With N bits in the blocksig, there is a 1/2\string^N chance per}}
\DoxyCodeLine{00153 \textcolor{comment}{       comparison of a hash colision. So with 2\string^N attempts there would be a}}
\DoxyCodeLine{00154 \textcolor{comment}{       fair chance of having a collision. So we want to round up to the next}}
\DoxyCodeLine{00155 \textcolor{comment}{       byte, add an extra 2 bytes (16 bits) in the strongsum, and assume the}}
\DoxyCodeLine{00156 \textcolor{comment}{       weaksum is worth another 16 bits, for at least 32 bits extra, giving a}}
\DoxyCodeLine{00157 \textcolor{comment}{       worst case 1/2\string^32 chance of having a hash collision per delta. If}}
\DoxyCodeLine{00158 \textcolor{comment}{       old\_fsize is unknown we use a conservative default. */}}
\DoxyCodeLine{00159     \textcolor{keywordflow}{if} (old\_fsize < 0) \{}
\DoxyCodeLine{00160         min\_strong\_len = RS\_DEFAULT\_MIN\_STRONG\_LEN;}
\DoxyCodeLine{00161     \} \textcolor{keywordflow}{else} \{}
\DoxyCodeLine{00162         min\_strong\_len =}
\DoxyCodeLine{00163             2 + (rs\_long\_ln2(old\_fsize + ((rs\_long\_t)1 << 24)) +}
\DoxyCodeLine{00164                  rs\_long\_ln2(old\_fsize / *block\_len + 1) + 7) / 8;}
\DoxyCodeLine{00165     \}}
\DoxyCodeLine{00166     \textcolor{keywordflow}{if} (*strong\_len == 0)}
\DoxyCodeLine{00167         *strong\_len = max\_strong\_len;}
\DoxyCodeLine{00168     \textcolor{keywordflow}{else} \textcolor{keywordflow}{if} (*strong\_len == -\/1)}
\DoxyCodeLine{00169         *strong\_len = min\_strong\_len;}
\DoxyCodeLine{00170     \textcolor{keywordflow}{else} \textcolor{keywordflow}{if} (old\_fsize >= 0 \&\& *strong\_len < min\_strong\_len) \{}
\DoxyCodeLine{00171         rs\_warn(\textcolor{stringliteral}{"{}strong\_len="{}} FMT\_SIZE \textcolor{stringliteral}{"{} smaller than recommended minimum "{}}}
\DoxyCodeLine{00172                 FMT\_SIZE \textcolor{stringliteral}{"{} for old\_fsize="{}} FMT\_LONG \textcolor{stringliteral}{"{} with block\_len="{}} FMT\_SIZE,}
\DoxyCodeLine{00173                 *strong\_len, min\_strong\_len, old\_fsize, *block\_len);}
\DoxyCodeLine{00174     \} \textcolor{keywordflow}{else} \textcolor{keywordflow}{if} (*strong\_len > max\_strong\_len) \{}
\DoxyCodeLine{00175         rs\_error(\textcolor{stringliteral}{"{}invalid strong\_len="{}} FMT\_SIZE \textcolor{stringliteral}{"{} for magic=\%\#x"{}}, *strong\_len,}
\DoxyCodeLine{00176                  (\textcolor{keywordtype}{int})*magic);}
\DoxyCodeLine{00177         \textcolor{keywordflow}{return} RS\_PARAM\_ERROR;}
\DoxyCodeLine{00178     \}}
\DoxyCodeLine{00179     rs\_sig\_args\_check(*magic, *block\_len, *strong\_len);}
\DoxyCodeLine{00180     \textcolor{keywordflow}{return} RS\_DONE;}
\DoxyCodeLine{00181 \}}
\DoxyCodeLine{00182 }
\DoxyCodeLine{00183 rs\_result rs\_signature\_init(rs\_signature\_t *sig, rs\_magic\_number magic,}
\DoxyCodeLine{00184                             \textcolor{keywordtype}{size\_t} block\_len, \textcolor{keywordtype}{size\_t} strong\_len,}
\DoxyCodeLine{00185                             rs\_long\_t sig\_fsize)}
\DoxyCodeLine{00186 \{}
\DoxyCodeLine{00187     rs\_result result;}
\DoxyCodeLine{00188 }
\DoxyCodeLine{00189     \textcolor{comment}{/* Check and set default arguments, using old\_fsize=-\/1 for unknown. */}}
\DoxyCodeLine{00190     \textcolor{keywordflow}{if} ((result = rs\_sig\_args(-\/1, \&magic, \&block\_len, \&strong\_len)) != RS\_DONE)}
\DoxyCodeLine{00191         \textcolor{keywordflow}{return} result;}
\DoxyCodeLine{00192     \textcolor{comment}{/* Set attributes from args. */}}
\DoxyCodeLine{00193     sig-\/>magic = magic;}
\DoxyCodeLine{00194     sig-\/>block\_len = (int)block\_len;}
\DoxyCodeLine{00195     sig-\/>strong\_sum\_len = (int)strong\_len;}
\DoxyCodeLine{00196     sig-\/>count = 0;}
\DoxyCodeLine{00197     \textcolor{comment}{/* Calculate the number of blocks if we have the signature file size. */}}
\DoxyCodeLine{00198     \textcolor{comment}{/* Magic+header is 12 bytes, each block thereafter is 4 bytes}}
\DoxyCodeLine{00199 \textcolor{comment}{       weak\_sum+strong\_sum\_len bytes */}}
\DoxyCodeLine{00200     sig-\/>size = (int)(sig\_fsize < 12 ? 0 : (sig\_fsize -\/ 12) / (4 + strong\_len));}
\DoxyCodeLine{00201     \textcolor{keywordflow}{if} (sig-\/>size)}
\DoxyCodeLine{00202         sig-\/>block\_sigs =}
\DoxyCodeLine{00203             rs\_alloc(sig-\/>size * rs\_block\_sig\_size(sig),}
\DoxyCodeLine{00204                      \textcolor{stringliteral}{"{}signature-\/>block\_sigs"{}});}
\DoxyCodeLine{00205     \textcolor{keywordflow}{else}}
\DoxyCodeLine{00206         sig-\/>block\_sigs = NULL;}
\DoxyCodeLine{00207     sig-\/>hashtable = NULL;}
\DoxyCodeLine{00208 \textcolor{preprocessor}{\#ifndef HASHTABLE\_NSTATS}}
\DoxyCodeLine{00209     sig-\/>calc\_strong\_count = 0;}
\DoxyCodeLine{00210 \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{00211     rs\_signature\_check(sig);}
\DoxyCodeLine{00212     \textcolor{keywordflow}{return} RS\_DONE;}
\DoxyCodeLine{00213 \}}
\DoxyCodeLine{00214 }
\DoxyCodeLine{00215 \textcolor{keywordtype}{void} rs\_signature\_done(rs\_signature\_t *sig)}
\DoxyCodeLine{00216 \{}
\DoxyCodeLine{00217     hashtable\_free(sig-\/>hashtable);}
\DoxyCodeLine{00218     free(sig-\/>block\_sigs);}
\DoxyCodeLine{00219     rs\_bzero(sig, \textcolor{keyword}{sizeof}(*sig));}
\DoxyCodeLine{00220 \}}
\DoxyCodeLine{00221 }
\DoxyCodeLine{00222 rs\_block\_sig\_t *rs\_signature\_add\_block(rs\_signature\_t *sig,}
\DoxyCodeLine{00223                                        rs\_weak\_sum\_t weak\_sum,}
\DoxyCodeLine{00224                                        rs\_strong\_sum\_t *strong\_sum)}
\DoxyCodeLine{00225 \{}
\DoxyCodeLine{00226     rs\_signature\_check(sig);}
\DoxyCodeLine{00227     \textcolor{comment}{/* Apply mix32() to rollsum weaksums to improve their distribution. */}}
\DoxyCodeLine{00228     \textcolor{keywordflow}{if} (rs\_signature\_weaksum\_kind(sig) == RS\_ROLLSUM)}
\DoxyCodeLine{00229         weak\_sum = mix32(weak\_sum);}
\DoxyCodeLine{00230     \textcolor{comment}{/* If block\_sigs is full, allocate more space. */}}
\DoxyCodeLine{00231     \textcolor{keywordflow}{if} (sig-\/>count == sig-\/>size) \{}
\DoxyCodeLine{00232         sig-\/>size = sig-\/>size ? sig-\/>size * 2 : 16;}
\DoxyCodeLine{00233         sig-\/>block\_sigs =}
\DoxyCodeLine{00234             rs\_realloc(sig-\/>block\_sigs, sig-\/>size * rs\_block\_sig\_size(sig),}
\DoxyCodeLine{00235                        \textcolor{stringliteral}{"{}signature-\/>block\_sigs"{}});}
\DoxyCodeLine{00236     \}}
\DoxyCodeLine{00237     rs\_block\_sig\_t *b = rs\_block\_sig\_ptr(sig, sig-\/>count++);}
\DoxyCodeLine{00238     rs\_block\_sig\_init(b, weak\_sum, strong\_sum, sig-\/>strong\_sum\_len);}
\DoxyCodeLine{00239     \textcolor{keywordflow}{return} b;}
\DoxyCodeLine{00240 \}}
\DoxyCodeLine{00241 }
\DoxyCodeLine{00242 rs\_long\_t rs\_signature\_find\_match(rs\_signature\_t *sig, rs\_weak\_sum\_t weak\_sum,}
\DoxyCodeLine{00243                                   \textcolor{keywordtype}{void} \textcolor{keyword}{const} *buf, \textcolor{keywordtype}{size\_t} len)}
\DoxyCodeLine{00244 \{}
\DoxyCodeLine{00245     rs\_block\_match\_t m;}
\DoxyCodeLine{00246     rs\_block\_sig\_t *b;}
\DoxyCodeLine{00247 }
\DoxyCodeLine{00248     rs\_signature\_check(sig);}
\DoxyCodeLine{00249     rs\_block\_match\_init(\&m, sig, weak\_sum, NULL, buf, len);}
\DoxyCodeLine{00250     \textcolor{keywordflow}{if} ((b = hashtable\_find(sig-\/>hashtable, \&m))) \{}
\DoxyCodeLine{00251         \textcolor{keywordflow}{return} (rs\_long\_t)rs\_block\_sig\_idx(sig, b) * sig-\/>block\_len;}
\DoxyCodeLine{00252     \}}
\DoxyCodeLine{00253     \textcolor{keywordflow}{return} -\/1;}
\DoxyCodeLine{00254 \}}
\DoxyCodeLine{00255 }
\DoxyCodeLine{00256 \textcolor{keywordtype}{void} rs\_signature\_log\_stats(rs\_signature\_t \textcolor{keyword}{const} *sig)}
\DoxyCodeLine{00257 \{}
\DoxyCodeLine{00258 \textcolor{preprocessor}{\#ifndef HASHTABLE\_NSTATS}}
\DoxyCodeLine{00259     hashtable\_t *t = sig-\/>hashtable;}
\DoxyCodeLine{00260 }
\DoxyCodeLine{00261     rs\_log(RS\_LOG\_INFO | RS\_LOG\_NONAME,}
\DoxyCodeLine{00262            \textcolor{stringliteral}{"{}match statistics: signature[\%ld searches, \%ld (\%.3f\%\%) matches, "{}}}
\DoxyCodeLine{00263            \textcolor{stringliteral}{"{}\%ld (\%.3fx) weak sum compares, \%ld (\%.3f\%\%) strong sum compares, "{}}}
\DoxyCodeLine{00264            \textcolor{stringliteral}{"{}\%ld (\%.3f\%\%) strong sum calcs]"{}}, t-\/>find\_count, t-\/>match\_count,}
\DoxyCodeLine{00265            100.0 * (\textcolor{keywordtype}{double})t-\/>match\_count / (\textcolor{keywordtype}{double})t-\/>find\_count,}
\DoxyCodeLine{00266            t-\/>hashcmp\_count, (\textcolor{keywordtype}{double})t-\/>hashcmp\_count / (\textcolor{keywordtype}{double})t-\/>find\_count,}
\DoxyCodeLine{00267            t-\/>entrycmp\_count,}
\DoxyCodeLine{00268            100.0 * (\textcolor{keywordtype}{double})t-\/>entrycmp\_count / (\textcolor{keywordtype}{double})t-\/>find\_count,}
\DoxyCodeLine{00269            sig-\/>calc\_strong\_count,}
\DoxyCodeLine{00270            100.0 * (\textcolor{keywordtype}{double})sig-\/>calc\_strong\_count / (\textcolor{keywordtype}{double})t-\/>find\_count);}
\DoxyCodeLine{00271 \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{00272 \}}
\DoxyCodeLine{00273 }
\DoxyCodeLine{00274 rs\_result rs\_build\_hash\_table(rs\_signature\_t *sig)}
\DoxyCodeLine{00275 \{}
\DoxyCodeLine{00276     rs\_block\_match\_t m;}
\DoxyCodeLine{00277     rs\_block\_sig\_t *b;}
\DoxyCodeLine{00278     \textcolor{keywordtype}{int} i;}
\DoxyCodeLine{00279 }
\DoxyCodeLine{00280     rs\_signature\_check(sig);}
\DoxyCodeLine{00281     sig-\/>hashtable = hashtable\_new(sig-\/>count);}
\DoxyCodeLine{00282     \textcolor{keywordflow}{if} (!sig-\/>hashtable)}
\DoxyCodeLine{00283         \textcolor{keywordflow}{return} RS\_MEM\_ERROR;}
\DoxyCodeLine{00284     \textcolor{keywordflow}{for} (i = 0; i < sig-\/>count; i++) \{}
\DoxyCodeLine{00285         b = rs\_block\_sig\_ptr(sig, i);}
\DoxyCodeLine{00286         rs\_block\_match\_init(\&m, sig, b-\/>weak\_sum, \&b-\/>strong\_sum, NULL, 0);}
\DoxyCodeLine{00287         \textcolor{keywordflow}{if} (!hashtable\_find(sig-\/>hashtable, \&m))}
\DoxyCodeLine{00288             hashtable\_add(sig-\/>hashtable, b);}
\DoxyCodeLine{00289     \}}
\DoxyCodeLine{00290     hashtable\_stats\_init(sig-\/>hashtable);}
\DoxyCodeLine{00291     \textcolor{keywordflow}{return} RS\_DONE;}
\DoxyCodeLine{00292 \}}
\DoxyCodeLine{00293 }
\DoxyCodeLine{00294 \textcolor{keywordtype}{void} rs\_free\_sumset(rs\_signature\_t *psums)}
\DoxyCodeLine{00295 \{}
\DoxyCodeLine{00296     rs\_signature\_done(psums);}
\DoxyCodeLine{00297     free(psums);}
\DoxyCodeLine{00298 \}}
\DoxyCodeLine{00299 }
\DoxyCodeLine{00300 \textcolor{keywordtype}{void} rs\_sumset\_dump(rs\_signature\_t \textcolor{keyword}{const} *sums)}
\DoxyCodeLine{00301 \{}
\DoxyCodeLine{00302     \textcolor{keywordtype}{int} i;}
\DoxyCodeLine{00303     rs\_block\_sig\_t *b;}
\DoxyCodeLine{00304     \textcolor{keywordtype}{char} strong\_hex[RS\_MAX\_STRONG\_SUM\_LENGTH * 3];}
\DoxyCodeLine{00305 }
\DoxyCodeLine{00306     rs\_log(RS\_LOG\_INFO | RS\_LOG\_NONAME,}
\DoxyCodeLine{00307            \textcolor{stringliteral}{"{}sumset info: magic=\%\#x, block\_len=\%d, block\_num=\%d"{}}, sums-\/>magic,}
\DoxyCodeLine{00308            sums-\/>block\_len, sums-\/>count);}
\DoxyCodeLine{00309 }
\DoxyCodeLine{00310     \textcolor{keywordflow}{for} (i = 0; i < sums-\/>count; i++) \{}
\DoxyCodeLine{00311         b = rs\_block\_sig\_ptr(sums, i);}
\DoxyCodeLine{00312         rs\_hexify(strong\_hex, b-\/>strong\_sum, sums-\/>strong\_sum\_len);}
\DoxyCodeLine{00313         rs\_log(RS\_LOG\_INFO | RS\_LOG\_NONAME,}
\DoxyCodeLine{00314                \textcolor{stringliteral}{"{}sum \%6d: weak="{}} FMT\_WEAKSUM \textcolor{stringliteral}{"{}, strong=\%s"{}}, i, b-\/>weak\_sum,}
\DoxyCodeLine{00315                strong\_hex);}
\DoxyCodeLine{00316     \}}
\DoxyCodeLine{00317 \}}

\end{DoxyCode}
