示例#1
0
        /// <summary>
        /// Decode normally
        /// </summary>
        /// <returns></returns>
        public VCDiffResult Decode()
        {
            ByteBuffer instructionBuffer = new ByteBuffer(window.InstructionsAndSizesData);
            ByteBuffer addressBuffer     = new ByteBuffer(window.AddressesForCopyData);
            ByteBuffer addRunBuffer      = new ByteBuffer(window.AddRunData);

            InstructionDecoder instrDecoder = new InstructionDecoder(instructionBuffer, this.customTable);

            VCDiffResult result = VCDiffResult.SUCCESS;

            while (decodedOnly < window.DecodedDeltaLength && instructionBuffer.CanRead)
            {
                int  decodedSize = 0;
                byte mode        = 0;

                VCDiffInstructionType instruction = instrDecoder.Next(out decodedSize, out mode);

                switch (instruction)
                {
                case VCDiffInstructionType.EOD:
                    targetData.Clear();
                    return(VCDiffResult.EOD);

                case VCDiffInstructionType.ERROR:
                    targetData.Clear();
                    return(VCDiffResult.ERRROR);

                default:
                    break;
                }

                switch (instruction)
                {
                case VCDiffInstructionType.ADD:
                    result = DecodeAdd(decodedSize, addRunBuffer);
                    break;

                case VCDiffInstructionType.RUN:
                    result = DecodeRun(decodedSize, addRunBuffer);
                    break;

                case VCDiffInstructionType.COPY:
                    result = DecodeCopy(decodedSize, mode, addressBuffer);
                    break;

                default:
                    targetData.Clear();
                    return(VCDiffResult.ERRROR);
                }
            }

            if (window.HasChecksum)
            {
                uint adler = Checksum.ComputeAdler32(targetData.ToArray());

                if (adler != window.Checksum)
                {
                    result = VCDiffResult.ERRROR;
                }
            }

            targetData.Clear();
            return(result);
        }
示例#2
0
        /// <summary>
        /// Decode if as expecting interleave
        /// </summary>
        /// <returns></returns>
        public VCDiffResult DecodeInterleave()
        {
            VCDiffResult result = VCDiffResult.SUCCESS;
            //since interleave expected then the last point that was most likely decoded was the lengths section
            //so following is all data for the add run copy etc
            long                  interleaveLength       = window.InstructionAndSizesLength;
            List <byte>           previous               = new List <byte>();
            bool                  didBreakBeforeComplete = false;
            int                   lastDecodedSize        = 0;
            VCDiffInstructionType lastDecodedInstruction = VCDiffInstructionType.NOOP;

            while (interleaveLength > 0)
            {
                if (target.CanRead)
                {
                    //read in
                    didBreakBeforeComplete = false;

                    //try to read in all interleaved bytes
                    //if not then it will buffer for next time
                    previous.AddRange(target.ReadBytes((int)interleaveLength));
                    ByteBuffer incoming = new ByteBuffer(previous.ToArray());
                    previous.Clear();
                    long initialLength = incoming.Length;

                    InstructionDecoder instrDecoder = new InstructionDecoder(incoming, this.customTable);

                    while (incoming.CanRead && decodedOnly < window.DecodedDeltaLength)
                    {
                        int  decodedSize = 0;
                        byte mode        = 0;
                        VCDiffInstructionType instruction = VCDiffInstructionType.NOOP;

                        if (lastDecodedSize > 0 && lastDecodedInstruction != VCDiffInstructionType.NOOP)
                        {
                            decodedSize = lastDecodedSize;
                            instruction = lastDecodedInstruction;
                        }
                        else
                        {
                            instruction = instrDecoder.Next(out decodedSize, out mode);

                            switch (instruction)
                            {
                            case VCDiffInstructionType.EOD:
                                didBreakBeforeComplete = true;
                                break;

                            case VCDiffInstructionType.ERROR:
                                targetData.Clear();
                                return(VCDiffResult.ERRROR);

                            default:
                                break;
                            }
                        }

                        //if instruction is EOD then decodedSize will be 0 as well
                        //the last part of the buffer containing the instruction will be
                        //buffered for the next loop
                        lastDecodedInstruction = instruction;
                        lastDecodedSize        = decodedSize;

                        if (didBreakBeforeComplete)
                        {
                            //we don't have all the data so store this pointer into a temporary list to resolve next loop
                            didBreakBeforeComplete = true;
                            interleaveLength      -= incoming.Position;

                            if (initialLength - incoming.Position > 0)
                            {
                                previous.AddRange(incoming.ReadBytes((int)(initialLength - incoming.Position)));
                            }

                            break;
                        }

                        switch (instruction)
                        {
                        case VCDiffInstructionType.ADD:
                            result = DecodeAdd(decodedSize, incoming);
                            break;

                        case VCDiffInstructionType.RUN:
                            result = DecodeRun(decodedSize, incoming);
                            break;

                        case VCDiffInstructionType.COPY:
                            result = DecodeCopy(decodedSize, mode, incoming);
                            break;

                        default:
                            targetData.Clear();
                            return(VCDiffResult.ERRROR);
                        }

                        if (result == VCDiffResult.EOD)
                        {
                            //we don't have all the data so store this pointer into a temporary list to resolve next loop
                            didBreakBeforeComplete = true;
                            interleaveLength      -= incoming.Position;

                            if (initialLength - incoming.Position > 0)
                            {
                                previous.AddRange(incoming.ReadBytes((int)(initialLength - incoming.Position)));
                            }

                            break;
                        }

                        //reset these as we have successfully used them
                        lastDecodedInstruction = VCDiffInstructionType.NOOP;
                        lastDecodedSize        = 0;
                    }

                    if (!didBreakBeforeComplete)
                    {
                        interleaveLength -= initialLength;
                    }
                }
            }

            if (window.HasChecksum)
            {
                uint adler = Checksum.ComputeAdler32(targetData.ToArray());

                if (adler != window.Checksum)
                {
                    result = VCDiffResult.ERRROR;
                }
            }

            targetData.Clear();
            return(result);
        }
示例#3
0
        /// <summary>
        /// Encodes the data using the settings from initialization
        /// </summary>
        /// <param name="newData">the target data</param>
        /// <param name="sout">the out stream</param>
        public void EncodeChunk(IByteBuffer newData, ByteStreamWriter sout)
        {
            uint checksum = 0;

            ///If checksum needed
            ///Generate Adler32 checksum for the incoming bytes
            if (hasChecksum)
            {
                newData.Position = 0;
                byte[] bytes = newData.ReadBytes((int)newData.Length);
                checksum = Checksum.ComputeAdler32(bytes);

                bytes = null;
            }

            windowEncoder = new WindowEncoder(oldData.Length, checksum, interleaved, hasChecksum);

            oldData.Position = 0;
            newData.Position = 0;

            this.newData = newData;
            long nextEncode       = newData.Position;
            long targetEnd        = newData.Length;
            long startOfLastBlock = targetEnd - BlockHash.BlockSize;
            long candidatePos     = nextEncode;

            //create the first hash
            ulong hash = hasher.Hash(newData.PeekBytes(BlockHash.BlockSize));

            while (true)
            {
                //if less than block size exit and then write as an ADD
                if (newData.Length - nextEncode < BlockHash.BlockSize)
                {
                    break;
                }

                //try and encode the copy and add instructions that best match
                long bytesEncoded = EncodeCopyForBestMatch(hash, candidatePos, nextEncode, targetEnd);

                if (bytesEncoded > 0)
                {
                    nextEncode  += bytesEncoded;
                    candidatePos = nextEncode;

                    if (candidatePos > startOfLastBlock)
                    {
                        break;
                    }

                    newData.Position = candidatePos;
                    //cannot use rolling hash since we skipped so many
                    hash = hasher.Hash(newData.ReadBytes(BlockHash.BlockSize));
                }
                else
                {
                    if (candidatePos + 1 > startOfLastBlock)
                    {
                        break;
                    }

                    //update hash requires the first byte of the last hash as well as the byte that is first byte pos + blockSize
                    //in order to properly calculate the rolling hash
                    newData.Position = candidatePos;
                    byte peek0 = newData.ReadByte();
                    newData.Position = candidatePos + BlockHash.BlockSize;
                    byte peek1 = newData.ReadByte();
                    hash = hasher.UpdateHash(hash, peek0, peek1);
                    candidatePos++;
                }
            }

            //Add the rest of the data that was not encoded
            if (nextEncode < newData.Length)
            {
                int len = (int)(newData.Length - nextEncode);
                newData.Position = nextEncode;
                windowEncoder.Add(newData.ReadBytes(len));
            }

            //output the final window
            windowEncoder.Output(sout);
        }