Esempio n. 1
0
        /// <summary>
        /// Calculate and write a diff for the file.
        /// </summary>
        /// <param name="interleaved">Whether to output in SDCH interleaved diff format.</param>
        /// <param name="checksumFormat">
        /// Whether to include Adler32 checksums for encoded data windows. If interleaved is true, <see cref="ChecksumFormat.Xdelta3"/>
        /// is not supported.
        /// </param>
        /// <returns>
        /// <see cref="VCDiffResult.SUCCESS"/> if successful, <see cref="VCDiffResult.ERROR"/> if the sourceStream or target are zero-length.</returns>
        /// <exception cref="ArgumentException">If interleaved is true, and <see cref="ChecksumFormat.Xdelta3"/> is chosen.</exception>
        public VCDiffResult Encode(bool interleaved = false,
                                   ChecksumFormat checksumFormat = ChecksumFormat.None)
        {
            if (oldData == null)
            {
                this.oldData = new ByteBuffer(sourceStream);
            }

            if (interleaved && checksumFormat == ChecksumFormat.Xdelta3)
            {
                throw new ArgumentException("Interleaved diffs can not have an xdelta3 checksum!");
            }

            if (targetData.Length == 0 || oldData.Length == 0)
            {
                return(VCDiffResult.ERROR);
            }

            VCDiffResult result = VCDiffResult.SUCCESS;

            oldData.Position    = 0;
            targetData.Position = 0;

            // file header
            // write magic bytes
            if (!interleaved && checksumFormat != ChecksumFormat.SDCH)
            {
                outputStream.Write(MagicBytes);
            }
            else
            {
                outputStream.Write(MagicBytesExtended);
            }

            //read in all the dictionary it is the only thing that needs to be
            BlockHash dictionary = new BlockHash(oldData, 0, hasher, blockSize);

            dictionary.AddAllBlocks();
            oldData.Position = 0;

            ChunkEncoder  chunker = new ChunkEncoder(dictionary, oldData, hasher, checksumFormat, interleaved, chunkSize);
            Memory <byte> buf     = new Memory <byte>(new byte[bufferSize]);
            Span <byte>   bufSpan = buf.Span;

            while (targetData.CanRead)
            {
                int bytesRead = targetData.ReadBytesIntoBuf(bufSpan);
                using ByteBuffer ntarget = new ByteBuffer(buf[..bytesRead]);
Esempio n. 2
0
        /// <summary>
        /// Calculate and write a diff for the file.
        /// </summary>
        /// <param name="interleaved">Whether to output in SDCH interleaved diff format.</param>
        /// <param name="checksumFormat">
        /// Whether to include Adler32 checksums for encoded data windows. If interleaved is true, <see cref="ChecksumFormat.Xdelta3"/>
        /// is not supported.
        /// </param>
        /// <param name="progress">Reports an estimate of the encoding progress. Value if 0 to 1.</param>
        /// <returns>
        /// <see cref="VCDiffResult.SUCCESS"/> if successful, <see cref="VCDiffResult.ERROR"/> if the sourceStream or target are zero-length.</returns>
        /// <exception cref="ArgumentException">If interleaved is true, and <see cref="ChecksumFormat.Xdelta3"/> is chosen.</exception>
        public VCDiffResult Encode(bool interleaved = false, ChecksumFormat checksumFormat = ChecksumFormat.None, IProgress <float>?progress = null)
        {
            Task WriteBytes(byte[] bytes)
            {
                outputStream.Write(bytes);
                return(Task.CompletedTask);
            }

            ValidateParameters(interleaved, checksumFormat);
            if (!Encode_Init(interleaved, checksumFormat, WriteBytes).Result)
            {
                return(VCDiffResult.ERROR);
            }

            // Read in all the dictionary it is the only thing that needs to be
            Encode_Setup(interleaved, checksumFormat, out var chunker, out var buf);
            try
            {
                var bufSpan = buf.Span;
                while (targetData.CanRead)
                {
                    int bytesRead = targetData.ReadBytesIntoBuf(bufSpan);
                    using ByteBuffer ntarget = new ByteBuffer(buf[..bytesRead]);