Esempio n. 1
0
        private bool MatchesPart(YEncHeader header)
        {
            if (header.Part.HasValue)
            {
                return(Part.HasValue && header.Part.Value == Part.Value);
            }

            return(!Part.HasValue);
        }
Esempio n. 2
0
        public void Validate(YEncHeader header, MemoryBlockStream decodedPartStream)
        {
            if (!MatchesPart(header))
            {
                throw new YEncException($"Part mismatch. Expected {header.Part}, but got {Part}.");
            }

            if (Size != decodedPartStream.Length)
            {
                throw new YEncException($"Size mismatch. Expected {Size}, but got {decodedPartStream.Length}.");
            }

            var crc32 = Crc33 ?? PartCrc32;

            if (crc32.HasValue)
            {
                var calculatedCrc32 = Crc32.CalculateChecksum(decodedPartStream.ToArray());
                if (calculatedCrc32 != crc32.Value)
                {
                    throw new YEncException($"Checksum mismatch. Expected {crc32.Value}, but got {calculatedCrc32}.");
                }
            }
        }