Пример #1
0
        /// <summary>
        /// Function to assemble a file from delta and original and output it to the "output" file
        /// </summary>
        /// <param name="delta">Path to delta file</param>
        /// <param name="original">Path to original file</param>
        /// <param name="output">Path to output file</param>
        private static void DoDecode(string delta, string original, string output)
        {
            using (FileStream outputS = new FileStream(output, FileMode.Create, FileAccess.Write))
                using (FileStream dictS = new FileStream(original, FileMode.Open, FileAccess.Read))
                    using (FileStream targetS = new FileStream(delta, FileMode.Open, FileAccess.Read))
                    {
                        VCDecoder decoder = new VCDecoder(dictS, targetS, outputS);

                        //You must call decoder.Start() first. The header of the delta file must be available before calling decoder.Start()

                        VCDiffResult result = decoder.Start();

                        if (result != VCDiffResult.SUCCESS)
                        {
                            //error abort
                        }

                        result = decoder.Decode(out long bytesWritten);

                        if (result != VCDiffResult.SUCCESS)
                        {
                            //error decoding
                        }

                        //if success bytesWritten will contain the number of bytes that were decoded
                    }
        }
Пример #2
0
        void DoDecode(string outputFile, string oldFile, string patchFile)
        {
            using (FileStream target = new FileStream(patchFile, FileMode.Open, FileAccess.Read))
            {
                byte[] oldHash = new byte[20];
                byte[] newHash = new byte[20];
                target.Read(oldHash, 0, oldHash.Length);
                target.Read(newHash, 0, newHash.Length);

                byte[] realHash = GetSha1FromFile(oldFile);

                bool oldHashMatches = CompareHashes(oldHash, realHash);

                if (!oldHashMatches)
                {
                    if (CompareHashes(realHash, newHash))
                    {
                        File.Copy(oldFile, outputFile);
                        return;
                    }
                    else
                    {
                        throw new Exception("file hash mismatch");
                    }
                }


                using (FileStream dict = new FileStream(oldFile, FileMode.Open, FileAccess.Read))
                    using (FileStream output = new FileStream(outputFile, FileMode.Create, FileAccess.Write))
                    {
                        VCDecoder decoder = new VCDecoder(dict, target, output);

                        //You must call decoder.Start() first. The header of the delta file must be available before calling decoder.Start()

                        VCDiffResult result = decoder.Start();

                        if (result != VCDiffResult.SUCCESS)
                        {
                            //error abort
                            throw new Exception("abort while decoding");
                        }

                        long bytesWritten = 0;
                        result = decoder.Decode(out bytesWritten);

                        if (result != VCDiffResult.SUCCESS)
                        {
                            //error decoding
                            throw new Exception("Error decoding");
                        }

                        //if success bytesWritten will contain the number of bytes that were decoded
                    }
            }
        }
Пример #3
0
        private static VCDiffResult Decode(Stream sold, Stream sdelta, Stream sout)
        {
            VCDecoder decoder = new VCDecoder(sold, sdelta, sout);

            var result = decoder.Start();

            if (result != VCDiffResult.Succes)
            {
                return(result);
            }

            return(decoder.Decode(out _));
        }
Пример #4
0
        /// <summary>
        /// Do not run on main thread
        /// </summary>
        /// <param name="id"></param>
        /// <param name="region"></param>
        public static bool Merge(int id, string region)
        {
            if (!HasLocalData(id, region) || !HasLocalDataDiff(id, region))
            {
                return(false);
            }

            byte[] dictData  = GetLocalData(id, region);
            byte[] deltaData = GetLocalDataDiff(id, region);

            if (dictData == null || deltaData == null)
            {
                return(false);
            }

            try
            {
                using (ByteBuffer dictBuffer = new ByteBuffer(dictData))
                    using (ByteBuffer deltaBuffer = new ByteBuffer(deltaData))
                        using (MemoryStream ms = new MemoryStream())
                        {
                            VCDecoder    decoder = new VCDecoder(dictBuffer, deltaBuffer, ms);
                            VCDiffResult result  = decoder.Start();

                            if (result != VCDiffResult.SUCCESS)
                            {
                                return(false);
                            }

                            long bytesWritten = 0;
                            result = decoder.Decode(out bytesWritten);

                            if (result != VCDiffResult.SUCCESS)
                            {
                                return(false);
                            }

                            string fname = string.Format(id >= 0 ? DB_FILE : DB_REGION_FILE, region, id);
                            string fpath = Path.Combine(DirectoryPath, fname);
                            File.WriteAllBytes(fpath, ms.ToArray());
                            return(true);
                        }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
            }

            return(false);
        }
Пример #5
0
        internal static void MergeDiffToDat(Stream origStream, Stream modifiedStream, Stream mergedStream)
        {
            VCDecoder    decoder = new VCDecoder(origStream, modifiedStream, mergedStream);
            VCDiffResult result  = decoder.Start(); //encodes with no checksum and not interleaved

            if (result != VCDiffResult.SUCCESS)
            {
                //error was not able to encode properly
            }
            else
            {
                long bytesWritten = 0;
                result = decoder.Decode(out bytesWritten);

                if (result != VCDiffResult.SUCCESS)
                {
                }
            }
        }
Пример #6
0
        public void TestEncodeAndDecodeShouldBeTheSame()
        {
            int size = 20 * 1024 * 1024; // 20 MB

            byte[] oldData = CreateRandomByteArray(size);
            byte[] newData = new byte[size];

            oldData.CopyTo(newData, 0);

            AddRandomPiecesIn(oldData);

            var sOld   = new MemoryStream(oldData);
            var sNew   = new MemoryStream(newData);
            var sDelta = new MemoryStream(new byte[size], true);

            var coder = new VCCoder(sOld, sNew, sDelta);

            Assert.AreEqual(VCDiffResult.Succes, coder.Encode());

            TestContext.Out.WriteLine($"Delta is {sDelta.Position / 1024 / 1024} MB's");

            sDelta.SetLength(sDelta.Position);
            sDelta.Position = 0;
            sOld.Position   = 0;
            sNew.Position   = 0;

            var sPatched = new MemoryStream(new byte[size], true);

            var decoder = new VCDecoder(sOld, sDelta, sPatched);

            Assert.AreEqual(VCDiffResult.Succes, decoder.Start());
            Assert.AreEqual(VCDiffResult.Succes, decoder.Decode(out long bytesWritten));

            TestContext.Out.WriteLine($"Written {bytesWritten / 1024 / 1024} MB's");

            Assert.AreEqual(sNew.ToArray(), sPatched.ToArray());
        }