コード例 #1
0
        public static void BuildChecksum(Storage storage, string filePath)
        {
            var header = GetHeader(storage, filePath);
            var name   = Path.GetFileName(filePath);

            var checksum = new ChecksumInfo();

            using (var stream = storage.FileReadStream(filePath + ".header"))
            {
                checksum.Add("header", Hash.Generate(HashTypes.Sha512, stream));
            }

            using (var stream = storage.FileReadStream(filePath + ".data"))
            {
                checksum.Add("data", Hash.Generate(HashTypes.Sha512, stream));
            }

            if (header.Flags.HasFlag(DiscStorageFlags.SortedDynamicIndex) || header.Flags.HasFlag(DiscStorageFlags.UnsortedDynamicIndex))
            {
                using (var stream = storage.FileReadStream(filePath + ".lookup"))
                {
                    checksum.Add("lookup", Hash.Generate(HashTypes.Sha512, stream));
                }
            }

            storage.WriteFileBytes(filePath + ".checksums", checksum.ToArray());
        }
コード例 #2
0
        public static CheckDiscStorageResult CheckDiscStorage(Storage _storage, string path)
        {
            var checksumData = _storage.ReadFileBytes(path + ".checksums");

            if (checksumData != null)
            {
                var checksum = new ChecksumInfo(checksumData);
                var error    = false;
                using (var headerStream = _storage.FileReadStream(path + ".header"))
                {
                    var valid = checksum.Valid("header", headerStream);
                    if (!valid)
                    {
                        error = true;
                    }
                }

                using (var headerStream = _storage.FileReadStream(path + ".data"))
                {
                    var valid = checksum.Valid("data", headerStream);
                    if (!valid)
                    {
                        error = true;
                    }
                }

                if (checksum.Get("lookup") != null)
                {
                    using (var headerStream = _storage.FileReadStream(path + ".lookup"))
                    {
                        var valid = checksum.Valid("lookup", headerStream);
                        if (!valid)
                        {
                            error = true;
                        }
                    }
                }

                if (error)
                {
                    return(CheckDiscStorageResult.CheckumFailed);
                }
            }
            else
            {
                var error  = false;
                var header = GetHeader(_storage, path);
                if (header == null)
                {
                    return(CheckDiscStorageResult.HeaderNotFound);
                }

                using (var storage = new DiscStorage(_storage, path, header.BlockSize, header.UserData.Length, header.Flags | DiscStorageFlags.Readonly))
                {
                    for (var i = storage.StartIndex; i <= storage.EndIndex; i++)
                    {
                        if (!storage.BlockDataCrcValidRawIndex(i))
                        {
                            error = true;
                            break;
                        }
                    }
                }

                if (error)
                {
                    return(CheckDiscStorageResult.DataCrcError);
                }
                return(CheckDiscStorageResult.MissingChecksum);
            }

            return(CheckDiscStorageResult.Ok);
        }