\label{page_formats_md_doc_formats}%
\Hypertarget{page_formats_md_doc_formats}%
 \hypertarget{page_formats_autotoc_md56}{}\doxysubsection{Generalities}\label{page_formats_autotoc_md56}
There are two file formats used by {\ttfamily librsync} and {\ttfamily rdiff}\+: the {\itshape signature} file, which summarizes a data file, and the {\itshape delta} file, which describes the edits from one data file to another.

librsync does not know or care about any formats in the data files.

All integers are big-\/endian.\hypertarget{page_formats_autotoc_md57}{}\doxysubsection{Magic numbers}\label{page_formats_autotoc_md57}
All librsync files start with a u32 \mbox{\hyperlink{librsync_8h_ab6abafd1db0a3947bb3c076507f4f7c2}{rs\+\_\+magic\+\_\+number}} identifying them. These are declared in {\ttfamily \mbox{\hyperlink{librsync_8h}{librsync.\+h}}}, and there are different numbers for every different signature and delta file type. Note magic numbers for newer file types are not supported by older versions of librsync. Older librsync versions will immediately fail with an error when they encounter file types they don\textquotesingle{}t support.\hypertarget{page_formats_autotoc_md58}{}\doxysubsection{Signatures}\label{page_formats_autotoc_md58}
Signatures consist of a header followed by a number of block signatures for each block in the data file.

The signature header is\+: \begin{DoxyVerb}u32 magic;     // Some RS_*_SIG_MAGIC value.
u32 block_len; // Bytes per block.
u32 strong_sum_len;  // Bytes per strong sum in each block.
\end{DoxyVerb}
 Each block signature includes a weaksum followed by a truncated strongsum hash for one block of {\ttfamily block\+\_\+len} bytes from the input data file. The strongsum signature will be truncated to the {\ttfamily strong\+\_\+sum\+\_\+len} in the header. The final data block may be shorter. The number of blocks in the signature is therefore \begin{DoxyVerb}ceil(input_len/block_len)
\end{DoxyVerb}
 The block signature weak checksum is used as a rolling checksum to find moved data, and a strong hash used to check the match is correct. The weak checksum is either a rollsum (based on adler32) or (better alternative) rabinkarp, and the strong hash is either MD4 or BLAKE2 depending on the magic number.

Truncating the strongsum makes the signatures smaller at a cost of a greater chance of collisions. The strongsums are truncated by keeping the left most (first) bytes after computation.

Each signature block format is (see {\ttfamily rs\+\_\+sig\+\_\+do\+\_\+block})\+: \begin{DoxyVerb}u32 weak_sum;
u8[strong_sum_len] strong_sum;
\end{DoxyVerb}
 \hypertarget{page_formats_autotoc_md59}{}\doxysubsection{Delta files}\label{page_formats_autotoc_md59}
Deltas consist of the delta magic constant {\ttfamily RS\+\_\+\+DELTA\+\_\+\+MAGIC} followed by a series of commands. Commands tell the patch logic how to construct the result file (new version) from the basis file (old version).

There are three kinds of commands\+: the literal command, the copy command, and the end command. A command consists of a single byte followed by zero or more arguments. The number and size of the arguments are defined in {\ttfamily \mbox{\hyperlink{prototab_8c_source}{prototab.\+c}}}.

A literal command describes data not present in the basis file. It has one argument\+: {\ttfamily length}. The format is\+: \begin{DoxyVerb}u8 command; // in the range 0x41 through 0x44 inclusive
u8[arg1_len] length;
u8[length] data; // new data to append
\end{DoxyVerb}
 A copy command describes a range of data in the basis file. It has two arguments\+: {\ttfamily start} and {\ttfamily length}. The format is\+: \begin{DoxyVerb}u8 command; // in the range 0x45 through 0x54 inclusive
u8[arg1_len] start; // offset in the basis to begin copying data
u8[arg2_len] length; // number of bytes to copy from the basis
\end{DoxyVerb}
 The end command indicates the end of the delta file. It consists of a single null byte and has no arguments. 