예제 #1
0
 public static bool TryReadFrame(Stream source, out StorageFrameDecoded result)
 {
     result = default(StorageFrameDecoded);
     try
     {
         result = ReadFrame(source);
         return !result.IsEmpty;
     }
     catch (EndOfStreamException)
     {
         // we are done
         return false;
     }
     catch (Exception ex)
     {
         Trace.WriteLine(ex);
         // Auto-clean?
         return false;
     }
 }
예제 #2
0
        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;
            }
        }