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 } } }
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 _)); }
/// <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); }
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) { } } }