\doxysubsection{checksum.\+c}
\label{checksum_8c_source}\index{checksum.c@{checksum.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 -\/-\/ the 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) 1996 by Andrew Tridgell}}
\DoxyCodeLine{00007 \textcolor{comment}{ * Copyright (C) 1996 by Paul Mackerras}}
\DoxyCodeLine{00008 \textcolor{comment}{ *}}
\DoxyCodeLine{00009 \textcolor{comment}{ * This program is free software; you can redistribute it and/or modify}}
\DoxyCodeLine{00010 \textcolor{comment}{ * it under the terms of the GNU Lesser General Public License as published by}}
\DoxyCodeLine{00011 \textcolor{comment}{ * the Free Software Foundation; either version 2.1 of the License, or}}
\DoxyCodeLine{00012 \textcolor{comment}{ * (at your option) any later version.}}
\DoxyCodeLine{00013 \textcolor{comment}{ *}}
\DoxyCodeLine{00014 \textcolor{comment}{ * This program is distributed in the hope that it will be useful,}}
\DoxyCodeLine{00015 \textcolor{comment}{ * but WITHOUT ANY WARRANTY; without even the implied warranty of}}
\DoxyCodeLine{00016 \textcolor{comment}{ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the}}
\DoxyCodeLine{00017 \textcolor{comment}{ * GNU Lesser General Public License for more details.}}
\DoxyCodeLine{00018 \textcolor{comment}{ *}}
\DoxyCodeLine{00019 \textcolor{comment}{ * You should have received a copy of the GNU Lesser General Public License}}
\DoxyCodeLine{00020 \textcolor{comment}{ * along with this program; if not, write to the Free Software}}
\DoxyCodeLine{00021 \textcolor{comment}{ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.}}
\DoxyCodeLine{00022 \textcolor{comment}{ */}}
\DoxyCodeLine{00023 }
\DoxyCodeLine{00024 \textcolor{preprocessor}{\#include "{}config.h"{}}             \textcolor{comment}{/* IWYU pragma: keep */}}
\DoxyCodeLine{00025 \textcolor{preprocessor}{\#include <stdint.h>}}
\DoxyCodeLine{00026 \textcolor{preprocessor}{\#include "{}checksum.h"{}}}
\DoxyCodeLine{00027 \textcolor{preprocessor}{\#include "{}blake2.h"{}}}
\DoxyCodeLine{00028 \textcolor{preprocessor}{\#include "{}librsync\_export.h"{}}}
\DoxyCodeLine{00029 }
\DoxyCodeLine{00030 LIBRSYNC\_EXPORT \textcolor{keyword}{const} \textcolor{keywordtype}{int} RS\_MD4\_SUM\_LENGTH = 16;}
\DoxyCodeLine{00031 LIBRSYNC\_EXPORT \textcolor{keyword}{const} \textcolor{keywordtype}{int} RS\_BLAKE2\_SUM\_LENGTH = 32;}
\DoxyCodeLine{00032 \textcolor{comment}{}}
\DoxyCodeLine{00033 \textcolor{comment}{/** A simple 32bit checksum that can be incrementally updated. */}}
\DoxyCodeLine{00034 rs\_weak\_sum\_t rs\_calc\_weak\_sum(weaksum\_kind\_t kind, \textcolor{keywordtype}{void} \textcolor{keyword}{const} *buf, \textcolor{keywordtype}{size\_t} len)}
\DoxyCodeLine{00035 \{}
\DoxyCodeLine{00036     \textcolor{keywordflow}{if} (kind == RS\_ROLLSUM) \{}
\DoxyCodeLine{00037         Rollsum sum;}
\DoxyCodeLine{00038         RollsumInit(\&sum);}
\DoxyCodeLine{00039         RollsumUpdate(\&sum, buf, len);}
\DoxyCodeLine{00040         \textcolor{keywordflow}{return} RollsumDigest(\&sum);}
\DoxyCodeLine{00041     \} \textcolor{keywordflow}{else} \{}
\DoxyCodeLine{00042         rabinkarp\_t sum;}
\DoxyCodeLine{00043         rabinkarp\_init(\&sum);}
\DoxyCodeLine{00044         rabinkarp\_update(\&sum, buf, len);}
\DoxyCodeLine{00045         \textcolor{keywordflow}{return} rabinkarp\_digest(\&sum);}
\DoxyCodeLine{00046     \}}
\DoxyCodeLine{00047 \}}
\DoxyCodeLine{00048 \textcolor{comment}{}}
\DoxyCodeLine{00049 \textcolor{comment}{/** Calculate and store into SUM a strong checksum.}}
\DoxyCodeLine{00050 \textcolor{comment}{ *}}
\DoxyCodeLine{00051 \textcolor{comment}{ * In plain rsync, the checksum is perturbed by a seed value. This is used when}}
\DoxyCodeLine{00052 \textcolor{comment}{ * retrying a failed transmission: we've discovered that the hashes collided at}}
\DoxyCodeLine{00053 \textcolor{comment}{ * some point, so we're going to try again with different hashes to see if we}}
\DoxyCodeLine{00054 \textcolor{comment}{ * can get it right. (Check tridge's thesis for details and to see if that's}}
\DoxyCodeLine{00055 \textcolor{comment}{ * correct.)}}
\DoxyCodeLine{00056 \textcolor{comment}{ *}}
\DoxyCodeLine{00057 \textcolor{comment}{ * Since we can't retry I'm not sure if it's very useful for librsync, except}}
\DoxyCodeLine{00058 \textcolor{comment}{ * perhaps as protection against hash collision attacks. */}}
\DoxyCodeLine{00059 \textcolor{keywordtype}{void} rs\_calc\_strong\_sum(strongsum\_kind\_t kind, \textcolor{keywordtype}{void} \textcolor{keyword}{const} *buf, \textcolor{keywordtype}{size\_t} len,}
\DoxyCodeLine{00060                         rs\_strong\_sum\_t *sum)}
\DoxyCodeLine{00061 \{}
\DoxyCodeLine{00062     \textcolor{keywordflow}{if} (kind == RS\_MD4) \{}
\DoxyCodeLine{00063         rs\_mdfour((\textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{char} *)sum, buf, len);}
\DoxyCodeLine{00064     \} \textcolor{keywordflow}{else} \{}
\DoxyCodeLine{00065         blake2b\_state ctx;}
\DoxyCodeLine{00066         blake2b\_init(\&ctx, RS\_MAX\_STRONG\_SUM\_LENGTH);}
\DoxyCodeLine{00067         blake2b\_update(\&ctx, (\textcolor{keyword}{const} uint8\_t *)buf, len);}
\DoxyCodeLine{00068         blake2b\_final(\&ctx, (uint8\_t *)sum, RS\_MAX\_STRONG\_SUM\_LENGTH);}
\DoxyCodeLine{00069     \}}
\DoxyCodeLine{00070 \}}

\end{DoxyCode}
