\doxysubsection{mksum.\+c}
\label{mksum_8c_source}\index{mksum.c@{mksum.c}}
\mbox{\hyperlink{mksum_8c}{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 -\/-\/ library for network deltas}}
\DoxyCodeLine{00004 \textcolor{comment}{ *}}
\DoxyCodeLine{00005 \textcolor{comment}{ * Copyright 1999-\/2001, 2014, 2015 by Martin Pool <mbp@sourcefrog.net>}}
\DoxyCodeLine{00006 \textcolor{comment}{ * Copyright (C) 1999 by Andrew Tridgell <tridge@samba.org>}}
\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 \textcolor{comment}{}}
\DoxyCodeLine{00023 \textcolor{comment}{/** \(\backslash\)file mksum.c}}
\DoxyCodeLine{00024 \textcolor{comment}{ * Generate file signatures.}}
\DoxyCodeLine{00025 \textcolor{comment}{ *}}
\DoxyCodeLine{00026 \textcolor{comment}{ * Generating checksums is pretty easy, since we can always just process}}
\DoxyCodeLine{00027 \textcolor{comment}{ * whatever data is available. When a whole block has arrived, or we've reached}}
\DoxyCodeLine{00028 \textcolor{comment}{ * the end of the file, we write the checksum out. */}}
\DoxyCodeLine{00029 }
\DoxyCodeLine{00030 \textcolor{preprocessor}{\#include "{}config.h"{}}             \textcolor{comment}{/* IWYU pragma: keep */}}
\DoxyCodeLine{00031 \textcolor{preprocessor}{\#include <stdlib.h>}}
\DoxyCodeLine{00032 \textcolor{preprocessor}{\#include "{}librsync.h"{}}}
\DoxyCodeLine{00033 \textcolor{preprocessor}{\#include "{}job.h"{}}}
\DoxyCodeLine{00034 \textcolor{preprocessor}{\#include "{}sumset.h"{}}}
\DoxyCodeLine{00035 \textcolor{preprocessor}{\#include "{}scoop.h"{}}}
\DoxyCodeLine{00036 \textcolor{preprocessor}{\#include "{}netint.h"{}}}
\DoxyCodeLine{00037 \textcolor{preprocessor}{\#include "{}trace.h"{}}}
\DoxyCodeLine{00038 \textcolor{preprocessor}{\#include "{}util.h"{}}}
\DoxyCodeLine{00039 }
\DoxyCodeLine{00040 \textcolor{comment}{/* Possible state functions for signature generation. */}}
\DoxyCodeLine{00041 \textcolor{keyword}{static} rs\_result rs\_sig\_s\_header(rs\_job\_t *);}
\DoxyCodeLine{00042 \textcolor{keyword}{static} rs\_result rs\_sig\_s\_generate(rs\_job\_t *);}
\DoxyCodeLine{00043 \textcolor{comment}{}}
\DoxyCodeLine{00044 \textcolor{comment}{/** State of trying to send the signature header. \(\backslash\)private */}}
\DoxyCodeLine{00045 \textcolor{keyword}{static} rs\_result rs\_sig\_s\_header(rs\_job\_t *job)}
\DoxyCodeLine{00046 \{}
\DoxyCodeLine{00047     rs\_signature\_t *sig = job-\/>signature;}
\DoxyCodeLine{00048     rs\_result result;}
\DoxyCodeLine{00049 }
\DoxyCodeLine{00050     \textcolor{keywordflow}{if} ((result =}
\DoxyCodeLine{00051          rs\_signature\_init(sig, job-\/>sig\_magic, job-\/>sig\_block\_len,}
\DoxyCodeLine{00052                            job-\/>sig\_strong\_len, 0)) != RS\_DONE)}
\DoxyCodeLine{00053         \textcolor{keywordflow}{return} result;}
\DoxyCodeLine{00054     rs\_squirt\_n4(job, sig-\/>magic);}
\DoxyCodeLine{00055     rs\_squirt\_n4(job, sig-\/>block\_len);}
\DoxyCodeLine{00056     rs\_squirt\_n4(job, sig-\/>strong\_sum\_len);}
\DoxyCodeLine{00057     rs\_trace(\textcolor{stringliteral}{"{}sent header (magic \%\#x, block len = \%d, strong sum len = \%d)"{}},}
\DoxyCodeLine{00058              sig-\/>magic, sig-\/>block\_len, sig-\/>strong\_sum\_len);}
\DoxyCodeLine{00059     job-\/>stats.block\_len = sig-\/>block\_len;}
\DoxyCodeLine{00060 }
\DoxyCodeLine{00061     job-\/>statefn = rs\_sig\_s\_generate;}
\DoxyCodeLine{00062     \textcolor{keywordflow}{return} RS\_RUNNING;}
\DoxyCodeLine{00063 \}}
\DoxyCodeLine{00064 \textcolor{comment}{}}
\DoxyCodeLine{00065 \textcolor{comment}{/** Generate the checksums for a block and write it out. Called when we}}
\DoxyCodeLine{00066 \textcolor{comment}{ * already know we have enough data in memory at \(\backslash\)p block. \(\backslash\)private */}}
\DoxyCodeLine{00067 \textcolor{keyword}{static} rs\_result rs\_sig\_do\_block(rs\_job\_t *job, \textcolor{keyword}{const} \textcolor{keywordtype}{void} *block, \textcolor{keywordtype}{size\_t} len)}
\DoxyCodeLine{00068 \{}
\DoxyCodeLine{00069     rs\_signature\_t *sig = job-\/>signature;}
\DoxyCodeLine{00070     rs\_weak\_sum\_t weak\_sum;}
\DoxyCodeLine{00071     rs\_strong\_sum\_t strong\_sum;}
\DoxyCodeLine{00072 }
\DoxyCodeLine{00073     weak\_sum = rs\_signature\_calc\_weak\_sum(sig, block, len);}
\DoxyCodeLine{00074     rs\_signature\_calc\_strong\_sum(sig, block, len, \&strong\_sum);}
\DoxyCodeLine{00075     rs\_squirt\_n4(job, weak\_sum);}
\DoxyCodeLine{00076     rs\_tube\_write(job, strong\_sum, sig-\/>strong\_sum\_len);}
\DoxyCodeLine{00077     \textcolor{keywordflow}{if} (rs\_trace\_enabled()) \{}
\DoxyCodeLine{00078         \textcolor{keywordtype}{char} strong\_sum\_hex[RS\_MAX\_STRONG\_SUM\_LENGTH * 2 + 1];}
\DoxyCodeLine{00079         rs\_hexify(strong\_sum\_hex, strong\_sum, sig-\/>strong\_sum\_len);}
\DoxyCodeLine{00080         rs\_trace(\textcolor{stringliteral}{"{}sent block: weak="{}} FMT\_WEAKSUM \textcolor{stringliteral}{"{}, strong=\%s"{}}, weak\_sum,}
\DoxyCodeLine{00081                  strong\_sum\_hex);}
\DoxyCodeLine{00082     \}}
\DoxyCodeLine{00083     job-\/>stats.sig\_blocks++;}
\DoxyCodeLine{00084     \textcolor{keywordflow}{return} RS\_RUNNING;}
\DoxyCodeLine{00085 \}}
\DoxyCodeLine{00086 \textcolor{comment}{}}
\DoxyCodeLine{00087 \textcolor{comment}{/** State of reading a block and trying to generate its sum. \(\backslash\)private */}}
\DoxyCodeLine{00088 \textcolor{keyword}{static} rs\_result rs\_sig\_s\_generate(rs\_job\_t *job)}
\DoxyCodeLine{00089 \{}
\DoxyCodeLine{00090     rs\_result result;}
\DoxyCodeLine{00091     \textcolor{keywordtype}{size\_t} len;}
\DoxyCodeLine{00092     \textcolor{keywordtype}{void} *block;}
\DoxyCodeLine{00093 }
\DoxyCodeLine{00094     \textcolor{comment}{/* must get a whole block, otherwise try again */}}
\DoxyCodeLine{00095     len = job-\/>signature-\/>block\_len;}
\DoxyCodeLine{00096     result = rs\_scoop\_read(job, len, \&block);}
\DoxyCodeLine{00097     \textcolor{comment}{/* If we are near EOF, get whatever is left. */}}
\DoxyCodeLine{00098     \textcolor{keywordflow}{if} (result == RS\_INPUT\_ENDED)}
\DoxyCodeLine{00099         result = rs\_scoop\_read\_rest(job, \&len, \&block);}
\DoxyCodeLine{00100     \textcolor{keywordflow}{if} (result == RS\_INPUT\_ENDED) \{}
\DoxyCodeLine{00101         \textcolor{keywordflow}{return} RS\_DONE;}
\DoxyCodeLine{00102     \} \textcolor{keywordflow}{else} \textcolor{keywordflow}{if} (result != RS\_DONE) \{}
\DoxyCodeLine{00103         rs\_trace(\textcolor{stringliteral}{"{}generate stopped: \%s"{}}, rs\_strerror(result));}
\DoxyCodeLine{00104         \textcolor{keywordflow}{return} result;}
\DoxyCodeLine{00105     \}}
\DoxyCodeLine{00106     rs\_trace(\textcolor{stringliteral}{"{}got "{}} FMT\_SIZE \textcolor{stringliteral}{"{} byte block"{}}, len);}
\DoxyCodeLine{00107     \textcolor{keywordflow}{return} rs\_sig\_do\_block(job, block, len);}
\DoxyCodeLine{00108 \}}
\DoxyCodeLine{00109 }
\DoxyCodeLine{00110 rs\_job\_t *rs\_sig\_begin(\textcolor{keywordtype}{size\_t} block\_len, \textcolor{keywordtype}{size\_t} strong\_len,}
\DoxyCodeLine{00111                        rs\_magic\_number sig\_magic)}
\DoxyCodeLine{00112 \{}
\DoxyCodeLine{00113     rs\_job\_t *job;}
\DoxyCodeLine{00114 }
\DoxyCodeLine{00115     job = rs\_job\_new(\textcolor{stringliteral}{"{}signature"{}}, rs\_sig\_s\_header);}
\DoxyCodeLine{00116     job-\/>signature = rs\_alloc\_struct(rs\_signature\_t);}
\DoxyCodeLine{00117     job-\/>job\_owns\_sig = 1;}
\DoxyCodeLine{00118     job-\/>sig\_magic = sig\_magic;}
\DoxyCodeLine{00119     job-\/>sig\_block\_len = (int)block\_len;}
\DoxyCodeLine{00120     job-\/>sig\_strong\_len = (int)strong\_len;}
\DoxyCodeLine{00121     \textcolor{keywordflow}{return} job;}
\DoxyCodeLine{00122 \}}

\end{DoxyCode}
