using System;
using System.Collections.Generic;
using System.Text;
namespace Duplicati.Library.SharpRSync
{
///
/// This class writes signature files in the same format as RDiff
///
public class ChecksumFileWriter
{
///
/// The length of a datablock
///
private int m_blocklen;
///
/// The number of bytes used for storing the strong signature
///
private int m_stronglen;
///
/// The re-used MD4 algorithm
///
private System.Security.Cryptography.HashAlgorithm m_hashAlgorithm;
///
/// The stream into which the signature data is written
///
private System.IO.Stream m_outstream;
///
/// Constructs a new CheckSum file with default sizes
///
/// The stream into which the checksum data is written
public ChecksumFileWriter(System.IO.Stream outputstream)
: this(outputstream, Adler32Checksum.DEFAULT_BLOCK_SIZE, 8)
{
}
///
/// Constructs a new CheckSum file
/// The length of a single block
/// The number of bytes in the MD4 checksum
/// The stream into which the checksum data is written
///
public ChecksumFileWriter(System.IO.Stream outputstream, int blocklength, int stronglength)
{
if (outputstream == null)
throw new ArgumentNullException("outputstream");
m_blocklen = blocklength;
m_stronglen = stronglength;
m_outstream = outputstream;
m_hashAlgorithm = Mono.Security.Cryptography.MD4.Create("MD4");
m_hashAlgorithm.Initialize();
m_outstream.Write(RDiffBinary.SIGNATURE_MAGIC, 0, RDiffBinary.SIGNATURE_MAGIC.Length);
m_outstream.Write(RDiffBinary.FixEndian(BitConverter.GetBytes(m_blocklen)), 0, 4);
m_outstream.Write(RDiffBinary.FixEndian(BitConverter.GetBytes(m_stronglen)), 0, 4);
}
///
/// Adds a chunck of data to checksum list
///
/// The data to add a checksum entry for
public void AddChunk(byte[] buffer)
{
AddChunk(buffer, 0, buffer.Length);
}
///
/// Adds a chunck of data to checksum list
///
/// The data to add a checksum entry for
/// The index in the buffer to start reading from
/// The number of bytes to extract from the array
public void AddChunk(byte[] buffer, int index, int count)
{
if (!m_hashAlgorithm.CanReuseTransform)
m_hashAlgorithm = Mono.Security.Cryptography.MD4.Create("MD4");
m_outstream.Write(RDiffBinary.FixEndian(BitConverter.GetBytes(Adler32Checksum.Calculate(buffer, index, count))), 0, 4);
m_outstream.Write(m_hashAlgorithm.ComputeHash(buffer, index, count), 0, m_stronglen);
}
///
/// Adds all checksums in an entire stream
///
/// The stream to read checksums from
public void AddStream(System.IO.Stream stream)
{
byte[] buffer = new byte[m_blocklen];
int a;
//These streams do not fragment data
if (stream is System.IO.FileStream || stream is System.IO.MemoryStream)
{
while ((a = stream.Read(buffer, 0, buffer.Length)) != 0)
AddChunk(buffer, 0, a);
}
else
{
while ((a = Utility.ForceStreamRead(stream, buffer, buffer.Length)) != 0)
AddChunk(buffer, 0, a);
}
}
///
/// Gets the number of bytes in each hashed block
///
public int BlockLength { get { return m_blocklen; } }
///
/// Gets the number of bytes in a signature file
///
public int StrongLength { get { return m_stronglen; } }
}
}