\doxysubsection{checksum.\+h}
\label{checksum_8h_source}\index{checksum.h@{checksum.h}}
\mbox{\hyperlink{checksum_8h}{Go to the documentation of this file.}}
\begin{DoxyCode}{0}
\DoxyCodeLine{00001 \textcolor{comment}{/*= -\/*-\/ c-\/basic-\/offset: 4; indent-\/tabs-\/mode: nil; -\/*-\/}}
\DoxyCodeLine{00002 \textcolor{comment}{ *}}
\DoxyCodeLine{00003 \textcolor{comment}{ * librsync -\/-\/ the library for network deltas}}
\DoxyCodeLine{00004 \textcolor{comment}{ *}}
\DoxyCodeLine{00005 \textcolor{comment}{ * Copyright (C) 2000, 2001 by Martin Pool <mbp@sourcefrog.net>}}
\DoxyCodeLine{00006 \textcolor{comment}{ *}}
\DoxyCodeLine{00007 \textcolor{comment}{ * This program is free software; you can redistribute it and/or modify}}
\DoxyCodeLine{00008 \textcolor{comment}{ * it under the terms of the GNU Lesser General Public License as published by}}
\DoxyCodeLine{00009 \textcolor{comment}{ * the Free Software Foundation; either version 2.1 of the License, or}}
\DoxyCodeLine{00010 \textcolor{comment}{ * (at your option) any later version.}}
\DoxyCodeLine{00011 \textcolor{comment}{ *}}
\DoxyCodeLine{00012 \textcolor{comment}{ * This program is distributed in the hope that it will be useful,}}
\DoxyCodeLine{00013 \textcolor{comment}{ * but WITHOUT ANY WARRANTY; without even the implied warranty of}}
\DoxyCodeLine{00014 \textcolor{comment}{ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the}}
\DoxyCodeLine{00015 \textcolor{comment}{ * GNU Lesser General Public License for more details.}}
\DoxyCodeLine{00016 \textcolor{comment}{ *}}
\DoxyCodeLine{00017 \textcolor{comment}{ * You should have received a copy of the GNU Lesser General Public License}}
\DoxyCodeLine{00018 \textcolor{comment}{ * along with this program; if not, write to the Free Software}}
\DoxyCodeLine{00019 \textcolor{comment}{ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.}}
\DoxyCodeLine{00020 \textcolor{comment}{ */}}
\DoxyCodeLine{00021 \textcolor{comment}{}}
\DoxyCodeLine{00022 \textcolor{comment}{/** \(\backslash\)file checksum.h}}
\DoxyCodeLine{00023 \textcolor{comment}{ * Abstract wrappers around different weaksum and strongsum implementations. */}}
\DoxyCodeLine{00024 \textcolor{preprocessor}{\#ifndef CHECKSUM\_H}}
\DoxyCodeLine{00025 \textcolor{preprocessor}{\#  define CHECKSUM\_H}}
\DoxyCodeLine{00026 }
\DoxyCodeLine{00027 \textcolor{preprocessor}{\#  include <assert.h>}}
\DoxyCodeLine{00028 \textcolor{preprocessor}{\#  include <stddef.h>}}
\DoxyCodeLine{00029 \textcolor{preprocessor}{\#  include "{}librsync.h"{}}}
\DoxyCodeLine{00030 \textcolor{preprocessor}{\#  include "{}rollsum.h"{}}}
\DoxyCodeLine{00031 \textcolor{preprocessor}{\#  include "{}rabinkarp.h"{}}}
\DoxyCodeLine{00032 \textcolor{preprocessor}{\#  include "{}hashtable.h"{}}}
\DoxyCodeLine{00033 \textcolor{comment}{}}
\DoxyCodeLine{00034 \textcolor{comment}{/** Weaksum implementations. */}}
\DoxyCodeLine{00035 \textcolor{keyword}{typedef} \textcolor{keyword}{enum} \{}
\DoxyCodeLine{00036     RS\_ROLLSUM,}
\DoxyCodeLine{00037     RS\_RABINKARP,}
\DoxyCodeLine{00038 \} weaksum\_kind\_t;}
\DoxyCodeLine{00039 \textcolor{comment}{}}
\DoxyCodeLine{00040 \textcolor{comment}{/** Strongsum implementations. */}}
\DoxyCodeLine{00041 \textcolor{keyword}{typedef} \textcolor{keyword}{enum} \{}
\DoxyCodeLine{00042     RS\_MD4,}
\DoxyCodeLine{00043     RS\_BLAKE2,}
\DoxyCodeLine{00044 \} strongsum\_kind\_t;}
\DoxyCodeLine{00045 \textcolor{comment}{}}
\DoxyCodeLine{00046 \textcolor{comment}{/** Abstract wrapper around weaksum implementations.}}
\DoxyCodeLine{00047 \textcolor{comment}{ *}}
\DoxyCodeLine{00048 \textcolor{comment}{ * This is a polymorphic interface to the different rollsum implementations.}}
\DoxyCodeLine{00049 \textcolor{comment}{ *}}
\DoxyCodeLine{00050 \textcolor{comment}{ * Historically rollsum methods were implemented as static inline functions}}
\DoxyCodeLine{00051 \textcolor{comment}{ * because they were small and needed to be fast. Now that we need to call}}
\DoxyCodeLine{00052 \textcolor{comment}{ * different methods for different rollsum implementations, they are getting}}
\DoxyCodeLine{00053 \textcolor{comment}{ * more complicated. Is it better to delegate calls to the right implementation}}
\DoxyCodeLine{00054 \textcolor{comment}{ * using static inline switch statements, or stop inlining them and use virtual}}
\DoxyCodeLine{00055 \textcolor{comment}{ * method pointers? Tests suggest inlined switch statements is faster. */}}
\DoxyCodeLine{00056 \textcolor{keyword}{typedef} \textcolor{keyword}{struct }weaksum \{}
\DoxyCodeLine{00057     weaksum\_kind\_t kind;}
\DoxyCodeLine{00058     \textcolor{keyword}{union }\{}
\DoxyCodeLine{00059         Rollsum rs;}
\DoxyCodeLine{00060         rabinkarp\_t rk;}
\DoxyCodeLine{00061     \} sum;}
\DoxyCodeLine{00062 \} weaksum\_t;}
\DoxyCodeLine{00063 }
\DoxyCodeLine{00064 \textcolor{keyword}{static} \textcolor{keyword}{inline} \textcolor{keywordtype}{void} weaksum\_reset(weaksum\_t *sum)}
\DoxyCodeLine{00065 \{}
\DoxyCodeLine{00066     \textcolor{keywordflow}{if} (sum-\/>kind == RS\_ROLLSUM)}
\DoxyCodeLine{00067         RollsumInit(\&sum-\/>sum.rs);}
\DoxyCodeLine{00068     \textcolor{keywordflow}{else}}
\DoxyCodeLine{00069         rabinkarp\_init(\&sum-\/>sum.rk);}
\DoxyCodeLine{00070 \}}
\DoxyCodeLine{00071 }
\DoxyCodeLine{00072 \textcolor{keyword}{static} \textcolor{keyword}{inline} \textcolor{keywordtype}{void} weaksum\_init(weaksum\_t *sum, weaksum\_kind\_t kind)}
\DoxyCodeLine{00073 \{}
\DoxyCodeLine{00074     assert(kind == RS\_ROLLSUM || kind == RS\_RABINKARP);}
\DoxyCodeLine{00075     sum-\/>kind = kind;}
\DoxyCodeLine{00076     weaksum\_reset(sum);}
\DoxyCodeLine{00077 \}}
\DoxyCodeLine{00078 }
\DoxyCodeLine{00079 \textcolor{keyword}{static} \textcolor{keyword}{inline} \textcolor{keywordtype}{size\_t} weaksum\_count(weaksum\_t *sum)}
\DoxyCodeLine{00080 \{}
\DoxyCodeLine{00081     \textcolor{comment}{/* We take advantage of sum-\/>sum.rs.count overlaying sum-\/>sum.rk.count. */}}
\DoxyCodeLine{00082     \textcolor{keywordflow}{return} sum-\/>sum.rs.count;}
\DoxyCodeLine{00083 \}}
\DoxyCodeLine{00084 }
\DoxyCodeLine{00085 \textcolor{keyword}{static} \textcolor{keyword}{inline} \textcolor{keywordtype}{void} weaksum\_update(weaksum\_t *sum, \textcolor{keyword}{const} \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{char} *buf,}
\DoxyCodeLine{00086                                   \textcolor{keywordtype}{size\_t} len)}
\DoxyCodeLine{00087 \{}
\DoxyCodeLine{00088     \textcolor{keywordflow}{if} (sum-\/>kind == RS\_ROLLSUM)}
\DoxyCodeLine{00089         RollsumUpdate(\&sum-\/>sum.rs, buf, len);}
\DoxyCodeLine{00090     \textcolor{keywordflow}{else}}
\DoxyCodeLine{00091         rabinkarp\_update(\&sum-\/>sum.rk, buf, len);}
\DoxyCodeLine{00092 \}}
\DoxyCodeLine{00093 }
\DoxyCodeLine{00094 \textcolor{keyword}{static} \textcolor{keyword}{inline} \textcolor{keywordtype}{void} weaksum\_rotate(weaksum\_t *sum, \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{char} out,}
\DoxyCodeLine{00095                                   \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{char} in)}
\DoxyCodeLine{00096 \{}
\DoxyCodeLine{00097     \textcolor{keywordflow}{if} (sum-\/>kind == RS\_ROLLSUM)}
\DoxyCodeLine{00098         RollsumRotate(\&sum-\/>sum.rs, out, in);}
\DoxyCodeLine{00099     \textcolor{keywordflow}{else}}
\DoxyCodeLine{00100         rabinkarp\_rotate(\&sum-\/>sum.rk, out, in);}
\DoxyCodeLine{00101 \}}
\DoxyCodeLine{00102 }
\DoxyCodeLine{00103 \textcolor{keyword}{static} \textcolor{keyword}{inline} \textcolor{keywordtype}{void} weaksum\_rollin(weaksum\_t *sum, \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{char} in)}
\DoxyCodeLine{00104 \{}
\DoxyCodeLine{00105     \textcolor{keywordflow}{if} (sum-\/>kind == RS\_ROLLSUM)}
\DoxyCodeLine{00106         RollsumRollin(\&sum-\/>sum.rs, in);}
\DoxyCodeLine{00107     \textcolor{keywordflow}{else}}
\DoxyCodeLine{00108         rabinkarp\_rollin(\&sum-\/>sum.rk, in);}
\DoxyCodeLine{00109 \}}
\DoxyCodeLine{00110 }
\DoxyCodeLine{00111 \textcolor{keyword}{static} \textcolor{keyword}{inline} \textcolor{keywordtype}{void} weaksum\_rollout(weaksum\_t *sum, \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{char} out)}
\DoxyCodeLine{00112 \{}
\DoxyCodeLine{00113     \textcolor{keywordflow}{if} (sum-\/>kind == RS\_ROLLSUM)}
\DoxyCodeLine{00114         RollsumRollout(\&sum-\/>sum.rs, out);}
\DoxyCodeLine{00115     \textcolor{keywordflow}{else}}
\DoxyCodeLine{00116         rabinkarp\_rollout(\&sum-\/>sum.rk, out);}
\DoxyCodeLine{00117 \}}
\DoxyCodeLine{00118 }
\DoxyCodeLine{00119 \textcolor{keyword}{static} \textcolor{keyword}{inline} rs\_weak\_sum\_t weaksum\_digest(weaksum\_t *sum)}
\DoxyCodeLine{00120 \{}
\DoxyCodeLine{00121     \textcolor{keywordflow}{if} (sum-\/>kind == RS\_ROLLSUM)}
\DoxyCodeLine{00122         \textcolor{comment}{/* We apply mix32() to rollsums before using them for matching. */}}
\DoxyCodeLine{00123         \textcolor{keywordflow}{return} mix32(RollsumDigest(\&sum-\/>sum.rs));}
\DoxyCodeLine{00124     \textcolor{keywordflow}{else}}
\DoxyCodeLine{00125         \textcolor{keywordflow}{return} rabinkarp\_digest(\&sum-\/>sum.rk);}
\DoxyCodeLine{00126 \}}
\DoxyCodeLine{00127 \textcolor{comment}{}}
\DoxyCodeLine{00128 \textcolor{comment}{/** Calculate a weaksum.}}
\DoxyCodeLine{00129 \textcolor{comment}{ *}}
\DoxyCodeLine{00130 \textcolor{comment}{ * Note this does not apply mix32() to rollsum digests, unlike}}
\DoxyCodeLine{00131 \textcolor{comment}{ * weaksum\_digest(). This is because rollsums are stored raw without mix32()}}
\DoxyCodeLine{00132 \textcolor{comment}{ * applied for backwards-\/compatibility, but we apply mix32() when adding them}}
\DoxyCodeLine{00133 \textcolor{comment}{ * into a signature and when getting the digest for calculating deltas. */}}
\DoxyCodeLine{00134 rs\_weak\_sum\_t rs\_calc\_weak\_sum(weaksum\_kind\_t kind, \textcolor{keywordtype}{void} \textcolor{keyword}{const} *buf,}
\DoxyCodeLine{00135                                \textcolor{keywordtype}{size\_t} len);}
\DoxyCodeLine{00136 \textcolor{comment}{}}
\DoxyCodeLine{00137 \textcolor{comment}{/** Calculate a strongsum. */}}
\DoxyCodeLine{00138 \textcolor{keywordtype}{void} rs\_calc\_strong\_sum(strongsum\_kind\_t kind, \textcolor{keywordtype}{void} \textcolor{keyword}{const} *buf, \textcolor{keywordtype}{size\_t} len,}
\DoxyCodeLine{00139                         rs\_strong\_sum\_t *sum);}
\DoxyCodeLine{00140 }
\DoxyCodeLine{00141 \textcolor{preprocessor}{\#endif                          }\textcolor{comment}{/* !CHECKSUM\_H */}\textcolor{preprocessor}{}}

\end{DoxyCode}
