// very manual writer... but want to see how small I can get the data. public static CompleteSignature ReadBinaryCompleteSignature(Stream s) { var sig = new CompleteSignature(); var l = new List<BlockSignature>(); var reader = new BinaryReader(s); int numberOfEntries = reader.ReadInt32(); for (var i = 0; i < numberOfEntries; i++) { var entry = new BlockSignature(); // 8 bytes. offset long offset = reader.ReadInt64(); // 4 bytes. size int size = reader.ReadInt32(); // 4 bytes. Block Number; int blockNumber = reader.ReadInt32(); // 4 bytes. Rolling Signature. decimal sig1 = reader.ReadDecimal(); decimal sig2 = reader.ReadDecimal(); RollingSignature rollingSig = new RollingSignature() { Sig1 = sig1, Sig2 = sig2 }; // should be 16 bytes. byte[] md5 = reader.ReadBytes(16); entry.BlockNumber = (UInt32)blockNumber; entry.RollingSig = (RollingSignature)rollingSig; entry.MD5Signature = md5; entry.Offset = offset; entry.Size = (uint)size; l.Add(entry); } sig.SignatureList = l.ToArray<BlockSignature>(); return sig; }
internal static BlockSignature GenerateBlockSig(byte[] buffer, long offset, int blockSize, uint id) { var sig = new BlockSignature(); var rollingSig = CreateRollingSignature(buffer, blockSize); var md5Sig = CreateMD5Signature(buffer, blockSize); sig.RollingSig = rollingSig; sig.MD5Signature = md5Sig; sig.Offset = offset; sig.BlockNumber = id; sig.Size = (uint) blockSize; return sig; }
// generates a dictionary with rolling sig as the key // BUT it assumes that the signatures passed as param are all of the same // signature size. internal static Dictionary<RollingSignature, List<BlockSignature>> GenerateBlockDict(BlockSignature[] sigArray) { var blockDict = new Dictionary<RollingSignature, List<BlockSignature>>(); List<BlockSignature> bsl; foreach (var element in sigArray) { if (blockDict.TryGetValue(element.RollingSig, out bsl)) { var addToList = false; // loop through sigs that have particular rolling sig and check for matching md5. foreach (var bs in bsl) { // if md5's are different then throw exception, if they're the same can keep proceeding. if (!bs.MD5Signature.SequenceEqual(element.MD5Signature)) { // sig already exists... can happen, but hopefully rare. addToList = true; } else { // matching md5.... so dont add to list. } } if (addToList) { bsl.Add(element); } } else { bsl = new List<BlockSignature>(); bsl.Add(element); blockDict[element.RollingSig] = bsl; } } return blockDict; }