mirror of
https://github.com/SteamRE/DepotDownloader.git
synced 2026-10-20 01:46:17 +00:00
Optimize calculating adler hashes to not allocate entire chunk in memory
This commit is contained in:
1 parent
e7e2299b70
commit
294dbff40a
2 files changed
+21
-35
No files matched your search
@@ -1059,10 +1059,18 @@ namespace DepotDownloader
|
|||||||
{
|
{
|
||||||
fsOld.Seek((long)match.OldChunk.Offset, SeekOrigin.Begin);
|
fsOld.Seek((long)match.OldChunk.Offset, SeekOrigin.Begin);
|
||||||
|
|
||||||
var tmp = new byte[match.OldChunk.UncompressedLength];
|
uint a = 0, b = 0;
|
||||||
fsOld.Read(tmp, 0, tmp.Length);
|
|
||||||
|
|
||||||
var adler = Util.AdlerHash(tmp);
|
for (var i = 0; i < match.OldChunk.UncompressedLength; i++)
|
||||||
|
{
|
||||||
|
var c = (uint)fsOld.ReadByte();
|
||||||
|
|
||||||
|
// adler hash
|
||||||
|
a = (a + c) % 65521;
|
||||||
|
b = (b + a) % 65521;
|
||||||
|
}
|
||||||
|
|
||||||
|
var adler = BitConverter.GetBytes(a | (b << 16));
|
||||||
if (!adler.SequenceEqual(match.OldChunk.Checksum))
|
if (!adler.SequenceEqual(match.OldChunk.Checksum))
|
||||||
{
|
{
|
||||||
neededChunks.Add(match.NewChunk);
|
neededChunks.Add(match.NewChunk);
|
||||||
@@ -1191,7 +1199,7 @@ namespace DepotDownloader
|
|||||||
var depot = depotFilesData.depotDownloadInfo;
|
var depot = depotFilesData.depotDownloadInfo;
|
||||||
var depotDownloadCounter = depotFilesData.depotCounter;
|
var depotDownloadCounter = depotFilesData.depotCounter;
|
||||||
|
|
||||||
var chunkID = Util.EncodeHexString(chunk.ChunkID);
|
var chunkID = Convert.ToHexString(chunk.ChunkID).ToLowerInvariant();
|
||||||
|
|
||||||
var data = new DepotManifest.ChunkData
|
var data = new DepotManifest.ChunkData
|
||||||
{
|
{
|
||||||
|
|||||||
+9
-31
@@ -78,26 +78,23 @@ namespace DepotDownloader
|
|||||||
public static List<ProtoManifest.ChunkData> ValidateSteam3FileChecksums(FileStream fs, ProtoManifest.ChunkData[] chunkdata)
|
public static List<ProtoManifest.ChunkData> ValidateSteam3FileChecksums(FileStream fs, ProtoManifest.ChunkData[] chunkdata)
|
||||||
{
|
{
|
||||||
var neededChunks = new List<ProtoManifest.ChunkData>();
|
var neededChunks = new List<ProtoManifest.ChunkData>();
|
||||||
int read;
|
|
||||||
|
|
||||||
foreach (var data in chunkdata)
|
foreach (var data in chunkdata)
|
||||||
{
|
{
|
||||||
var chunk = new byte[data.UncompressedLength];
|
|
||||||
fs.Seek((long)data.Offset, SeekOrigin.Begin);
|
fs.Seek((long)data.Offset, SeekOrigin.Begin);
|
||||||
read = fs.Read(chunk, 0, (int)data.UncompressedLength);
|
|
||||||
|
|
||||||
byte[] tempchunk;
|
uint a = 0, b = 0;
|
||||||
if (read < data.UncompressedLength)
|
|
||||||
|
for (var i = 0; i < data.UncompressedLength; i++)
|
||||||
{
|
{
|
||||||
tempchunk = new byte[read];
|
var c = (uint)fs.ReadByte();
|
||||||
Array.Copy(chunk, 0, tempchunk, 0, read);
|
|
||||||
}
|
// adler hash
|
||||||
else
|
a = (a + c) % 65521;
|
||||||
{
|
b = (b + a) % 65521;
|
||||||
tempchunk = chunk;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var adler = AdlerHash(tempchunk);
|
var adler = BitConverter.GetBytes(a | (b << 16));
|
||||||
if (!adler.SequenceEqual(data.Checksum))
|
if (!adler.SequenceEqual(data.Checksum))
|
||||||
{
|
{
|
||||||
neededChunks.Add(data);
|
neededChunks.Add(data);
|
||||||
@@ -107,18 +104,6 @@ namespace DepotDownloader
|
|||||||
return neededChunks;
|
return neededChunks;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] AdlerHash(byte[] input)
|
|
||||||
{
|
|
||||||
uint a = 0, b = 0;
|
|
||||||
for (var i = 0; i < input.Length; i++)
|
|
||||||
{
|
|
||||||
a = (a + input[i]) % 65521;
|
|
||||||
b = (b + a) % 65521;
|
|
||||||
}
|
|
||||||
|
|
||||||
return BitConverter.GetBytes(a | (b << 16));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static byte[] DecodeHexString(string hex)
|
public static byte[] DecodeHexString(string hex)
|
||||||
{
|
{
|
||||||
if (hex == null)
|
if (hex == null)
|
||||||
@@ -133,13 +118,6 @@ namespace DepotDownloader
|
|||||||
return bytes;
|
return bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string EncodeHexString(byte[] input)
|
|
||||||
{
|
|
||||||
return input.Aggregate(new StringBuilder(),
|
|
||||||
(sb, v) => sb.Append(v.ToString("x2"))
|
|
||||||
).ToString();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Decrypts using AES/ECB/PKCS7
|
/// Decrypts using AES/ECB/PKCS7
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in new issue
Block a user