Skip to content

Commit

Permalink
Fix padding issue on compressed zlib clips
Browse files Browse the repository at this point in the history
  • Loading branch information
neon-nyan committed Sep 28, 2023
1 parent d2132ac commit 9b47718
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
23 changes: 15 additions & 8 deletions Hi3Helper.SharpHDiffPatch/PatchDir/PatchDir.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ public void Patch(string input, string output, bool useBufferedPatch)

using (FileStream patchStream = spawnPatchStream())
{
patchStream.Position = (long)hdiffHeaderInfo.headDataOffset + padding;
padding = hdiffHeaderInfo.compMode == CompressionMode.zlib ? 1 : 0;
int headerPadding = hdiffHeaderInfo.headDataCompressedSize > 0 ? padding : 0;
patchStream.Position = hdiffHeaderInfo.headDataOffset + headerPadding;

PatchCore.GetDecompressStreamPlugin(hdiffHeaderInfo.compMode, patchStream, out Stream decompHeadStream,
(long)hdiffHeaderInfo.headDataSize, (long)hdiffHeaderInfo.headDataCompressedSize - padding, out _);
hdiffHeaderInfo.headDataSize, hdiffHeaderInfo.headDataCompressedSize - headerPadding, out _);

using (BinaryReader patchReader = new BinaryReader(patchStream))
using (BinaryReader patchDecompReader = new BinaryReader(decompHeadStream))
Expand All @@ -65,7 +68,7 @@ public void Patch(string input, string output, bool useBufferedPatch)
FileStream[] mergedOldStream = GetRefOldStreams(dirData).ToArray();
NewFileCombinedStreamStruct[] mergedNewStream = GetRefNewStreams(dirData).ToArray();

patchStream.Position = (long)hdiffHeaderInfo.hdiffDataOffset;
patchStream.Position = hdiffHeaderInfo.hdiffDataOffset;
_ = Header.TryParseHeaderInfo(patchReader, "", out _, out dirDiffInfo.hdiffinfo, out _);
padding = hdiffHeaderInfo.compMode == CompressionMode.zlib ? 1 : 0;

Expand Down Expand Up @@ -148,20 +151,24 @@ private void StartPatchRoutine(Stream inputStream, Stream outputStream, long new

try
{
clips[0] = PatchCore.GetBufferStreamFromOffset(dirDiffInfo.hdiffinfo.compMode, sourceClips[0], offset,
int coverPadding = dirDiffInfo.hdiffinfo.headInfo.compress_cover_buf_size > 0 ? padding : 0;
clips[0] = PatchCore.GetBufferStreamFromOffset(dirDiffInfo.hdiffinfo.compMode, sourceClips[0], offset + coverPadding,
dirDiffInfo.hdiffinfo.headInfo.cover_buf_size, dirDiffInfo.hdiffinfo.headInfo.compress_cover_buf_size, out long nextLength, this.useBufferedPatch);

offset += nextLength;
clips[1] = PatchCore.GetBufferStreamFromOffset(dirDiffInfo.hdiffinfo.compMode, sourceClips[1], offset,
int rle_ctrlBufPadding = dirDiffInfo.hdiffinfo.headInfo.compress_rle_ctrlBuf_size > 0 ? padding : 0;
clips[1] = PatchCore.GetBufferStreamFromOffset(dirDiffInfo.hdiffinfo.compMode, sourceClips[1], offset + rle_ctrlBufPadding,
dirDiffInfo.hdiffinfo.headInfo.rle_ctrlBuf_size, dirDiffInfo.hdiffinfo.headInfo.compress_rle_ctrlBuf_size, out nextLength, this.useBufferedPatch);

offset += nextLength;
clips[2] = PatchCore.GetBufferStreamFromOffset(dirDiffInfo.hdiffinfo.compMode, sourceClips[2], offset,
int rle_codeBufPadding = dirDiffInfo.hdiffinfo.headInfo.compress_rle_codeBuf_size > 0 ? padding : 0;
clips[2] = PatchCore.GetBufferStreamFromOffset(dirDiffInfo.hdiffinfo.compMode, sourceClips[2], offset + rle_codeBufPadding,
dirDiffInfo.hdiffinfo.headInfo.rle_codeBuf_size, dirDiffInfo.hdiffinfo.headInfo.compress_rle_codeBuf_size, out nextLength, this.useBufferedPatch);

offset += nextLength;
clips[3] = PatchCore.GetBufferStreamFromOffset(dirDiffInfo.hdiffinfo.compMode, sourceClips[3], offset,
dirDiffInfo.hdiffinfo.headInfo.newDataDiff_size, dirDiffInfo.hdiffinfo.headInfo.compress_newDataDiff_size, out _, false);
int newDataDiffPadding = dirDiffInfo.hdiffinfo.headInfo.compress_newDataDiff_size > 0 ? padding : 0;
clips[3] = PatchCore.GetBufferStreamFromOffset(dirDiffInfo.hdiffinfo.compMode, sourceClips[3], offset + newDataDiffPadding,
dirDiffInfo.hdiffinfo.headInfo.newDataDiff_size, dirDiffInfo.hdiffinfo.headInfo.compress_newDataDiff_size - padding, out _, false);

PatchCore.UncoverBufferClipsStream(clips, inputStream, outputStream, dirDiffInfo.hdiffinfo, newDataSize);
}
Expand Down
12 changes: 8 additions & 4 deletions Hi3Helper.SharpHDiffPatch/PatchSingle/PatchSingle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,23 @@ private void StartPatchRoutine(Stream inputStream, Stream outputStream)
try
{
long offset = hDiffInfo.headInfo.headEndPos;
clips[0] = PatchCore.GetBufferStreamFromOffset(hDiffInfo.compMode, sourceClips[0], offset + padding,
int coverPadding = hDiffInfo.headInfo.compress_cover_buf_size > 0 ? padding : 0;
clips[0] = PatchCore.GetBufferStreamFromOffset(hDiffInfo.compMode, sourceClips[0], offset + coverPadding,
hDiffInfo.headInfo.cover_buf_size, hDiffInfo.headInfo.compress_cover_buf_size, out long nextLength, this.isUseBufferedPatch);

offset += nextLength;
clips[1] = PatchCore.GetBufferStreamFromOffset(hDiffInfo.compMode, sourceClips[1], offset + padding,
int rle_ctrlBufPadding = hDiffInfo.headInfo.compress_rle_ctrlBuf_size > 0 ? padding : 0;
clips[1] = PatchCore.GetBufferStreamFromOffset(hDiffInfo.compMode, sourceClips[1], offset + rle_ctrlBufPadding,
hDiffInfo.headInfo.rle_ctrlBuf_size, hDiffInfo.headInfo.compress_rle_ctrlBuf_size, out nextLength, this.isUseBufferedPatch);

offset += nextLength;
clips[2] = PatchCore.GetBufferStreamFromOffset(hDiffInfo.compMode, sourceClips[2], offset + padding,
int rle_codeBufPadding = hDiffInfo.headInfo.compress_rle_codeBuf_size > 0 ? padding : 0;
clips[2] = PatchCore.GetBufferStreamFromOffset(hDiffInfo.compMode, sourceClips[2], offset + rle_codeBufPadding,
hDiffInfo.headInfo.rle_codeBuf_size, hDiffInfo.headInfo.compress_rle_codeBuf_size, out nextLength, this.isUseBufferedPatch);

offset += nextLength;
clips[3] = PatchCore.GetBufferStreamFromOffset(hDiffInfo.compMode, sourceClips[3], offset + padding,
int newDataDiffPadding = hDiffInfo.headInfo.compress_newDataDiff_size > 0 ? padding : 0;
clips[3] = PatchCore.GetBufferStreamFromOffset(hDiffInfo.compMode, sourceClips[3], offset + newDataDiffPadding,
hDiffInfo.headInfo.newDataDiff_size, hDiffInfo.headInfo.compress_newDataDiff_size - padding, out _, false);

PatchCore.UncoverBufferClipsStream(clips, inputStream, outputStream, hDiffInfo);
Expand Down

0 comments on commit 9b47718

Please sign in to comment.