Ejemplo n.º 1
0
        public async Task ApplyAsync(Stream basisFileStream, IDeltaReader delta, Stream outputStream)
        {
            var buffer = new byte[readBufferSize];

            await delta.ApplyAsync(
                writeData : async (data) => await outputStream.WriteAsync(data, 0, data.Length).ConfigureAwait(false),
                copy : async(startPosition, length) =>
            {
                basisFileStream.Seek(startPosition, SeekOrigin.Begin);

                int read;
                long soFar = 0;
                while ((read = await basisFileStream.ReadAsync(buffer, 0, (int)Math.Min(length - soFar, buffer.Length)).ConfigureAwait(false)) > 0)
                {
                    soFar += read;
                    await outputStream.WriteAsync(buffer, 0, read).ConfigureAwait(false);
                }
            }).ConfigureAwait(false);

            if (!SkipHashCheck)
            {
                if (!await HashCheckAsync(delta, outputStream).ConfigureAwait(false))
                {
                    throw new InvalidDataException(
                              $"Verification of the patched file failed. The {delta.Metadata.ExpectedFileHashAlgorithm} hash of the patch result file, and the file that was used as input for the delta, do not match. This can happen if the basis file changed since the signatures were calculated.");
                }
            }
        }
Ejemplo n.º 2
0
        public void Apply(Stream basisFileStream, IDeltaReader delta, Stream outputStream)
        {
            delta.Apply(
                writeData: (data) => outputStream.Write(data, 0, data.Length),
                copy: (startPosition, length) =>
            {
                basisFileStream.Seek(startPosition, SeekOrigin.Begin);

                var buffer = new byte[4 * 1024 * 1024];
                int read;
                long soFar = 0;
                while ((read = basisFileStream.Read(buffer, 0, (int)Math.Min(length - soFar, buffer.Length))) > 0)
                {
                    soFar += read;
                    outputStream.Write(buffer, 0, read);
                }
            });

            if (!SkipHashCheck)
            {
                outputStream.Seek(0, SeekOrigin.Begin);

                var sourceFileHash = delta.ExpectedHash;
                var algorithm      = delta.HashAlgorithm;

                var actualHash = algorithm.ComputeHash(outputStream);

                if (!BinaryComparer.CompareArray(sourceFileHash, actualHash))
                {
                    throw new UsageException("Verification of the patched file failed. The SHA1 hash of the patch result file, and the file that was used as input for the delta, do not match. This can happen if the basis file changed since the signatures were calculated.");
                }
            }
        }
Ejemplo n.º 3
0
        public void Apply(Stream basisFileStream, IDeltaReader delta, Stream outputStream)
        {
            var buffer = new byte[readBufferSize];

            delta.Apply(
                writeData: (data) => outputStream.Write(data, 0, data.Length),
                copy: (startPosition, length) =>
            {
                basisFileStream.Seek(startPosition, SeekOrigin.Begin);

                int read;
                long soFar = 0;
                while ((read = basisFileStream.Read(buffer, 0, (int)Math.Min(length - soFar, buffer.Length))) > 0)
                {
                    soFar += read;
                    outputStream.Write(buffer, 0, read);
                }
            });

            if (!SkipHashCheck)
            {
                if (!HashCheck(delta, outputStream))
                {
                    throw new InvalidDataException(
                              $"Verification of the patched file failed. The {delta.HashAlgorithm.Name} hash of the patch result file, and the file that was used as input for the delta, do not match. This can happen if the basis file changed since the signatures were calculated.");
                }
            }
        }
Ejemplo n.º 4
0
        public void Apply(Stream basisFileStream, IDeltaReader delta, Stream outputStream)
        {
            delta.Apply(
                writeData: (data) => outputStream.Write(data, 0, data.Length),
                copy: (startPosition, length) =>
                {
                    basisFileStream.Seek(startPosition, SeekOrigin.Begin);

                    var buffer = new byte[4*1024*1024];
                    int read;
                    do
                    {
                        read = basisFileStream.Read(buffer, 0, buffer.Length);

                        outputStream.Write(buffer, 0, read);
                    } while (read > 0);
                });

            if (!SkipHashCheck)
            {
                outputStream.Seek(0, SeekOrigin.Begin);

                var sourceFileHash = delta.ExpectedHash;
                var algorithm = delta.HashAlgorithm;

                var actualHash = algorithm.ComputeHash(outputStream);

                if (!StructuralComparisons.StructuralEqualityComparer.Equals(sourceFileHash, actualHash))
                    throw new UsageException("Verification of the patched file failed. The SHA1 hash of the patch result file, and the file that was used as input for the delta, do not match. This can happen if the basis file changed since the signatures were calculated.");
            }
        }
Ejemplo n.º 5
0
        public async Task <bool> HashCheckAsync(IDeltaReader delta, Stream outputStream)
        {
            outputStream.Seek(0, SeekOrigin.Begin);

            var sourceFileHash = delta.ExpectedHash;
            var algorithm      = SupportedAlgorithms.Hashing.Create(delta.Metadata.ExpectedFileHashAlgorithm);

            var actualHash = await algorithm.ComputeHashAsync(outputStream).ConfigureAwait(false);

            return(StructuralComparisons.StructuralEqualityComparer.Equals(sourceFileHash, actualHash));
        }
Ejemplo n.º 6
0
        public bool HashCheck(IDeltaReader delta, Stream outputStream)
        {
            outputStream.Seek(0, SeekOrigin.Begin);

            var sourceFileHash = delta.ExpectedHash;
            var algorithm      = SupportedAlgorithms.Hashing.Create(delta.Metadata.ExpectedFileHashAlgorithm);

            var actualHash = algorithm.ComputeHash(outputStream);

            return(StructuralComparisons.StructuralEqualityComparer.Equals(sourceFileHash, actualHash));
        }
Ejemplo n.º 7
0
        public void Apply(Stream basisFileStream, IDeltaReader delta, Stream outputStream, IProgressReporter progressReporter)
        {
            delta.Apply(
                writeData: (data, len) => outputStream.Write(data, 0, len),
                copy: (startPosition, length, buffer) =>
            {
                basisFileStream.Seek(startPosition, SeekOrigin.Begin);

                int read;
                long soFar = 0;
                while ((read = basisFileStream.Read(buffer, 0, (int)Math.Min(length - soFar, buffer.Length))) > 0)
                {
                    soFar += read;
                    outputStream.Write(buffer, 0, read);
                }
            });

            if (delta.FileSize != outputStream.Length)
            {
                throw new UsageException("Verification of the patched file failed. The hash of the patch result file, and the file that was used as input for the delta, do not match. This can happen if the basis file changed since the signatures were calculated.");
            }

            if (!SkipHashCheck)
            {
                outputStream.Seek(0, SeekOrigin.Begin);

                var sourceFileHash = delta.ExpectedHash;
                var algorithm      = delta.HashAlgorithm;
                var actualHash     = algorithm.ComputeHash(outputStream, progressReporter);
//                var actualHash2 = algorithm.ComputeHash(outputStream);

                if (!Enumerable.SequenceEqual(sourceFileHash, actualHash))
                {
                    throw new UsageException("Verification of the patched file failed. The hash of the patch result file, and the file that was used as input for the delta, do not match. This can happen if the basis file changed since the signatures were calculated.");
                }
            }
        }