public static StorageFrameDecoded ReadFrame(Stream source) { using (var binary = new BitReader(source)) { var version = binary.ReadInt64(); var name = binary.ReadString(); var len = binary.Read7BitInt(); var bytes = binary.ReadBytes(len); var sha1Expected = binary.ReadBytes(20); var decoded = new StorageFrameDecoded(bytes, name, version); if (decoded.IsEmpty && sha1Expected.All(b => b == 0)) { // this looks like end of the stream. return decoded; } //SHA1. TODO: compute hash nicely var sha1Actual = EncodeFrame(name, bytes, version).Hash; if (!sha1Expected.SequenceEqual(sha1Actual)) throw new StorageFrameException("SHA mismatch in data frame"); return decoded; } }
private long GetEndOffset() { if (!File.Exists(Path.Combine(_path, "stream.chk"))) return 0; using (_checkStream = new FileStream(Path.Combine(_path, "stream.chk"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (_checkBits = new BitReader(_checkStream)) { // calculate start and end offset //_checkStream.Seek(0, SeekOrigin.Begin); return _checkBits.ReadInt64(); } } }