diff --git a/OpenRA.Mods.Cnc/FileFormats/CRC32.cs b/OpenRA.Game/FileFormats/CRC32.cs similarity index 92% rename from OpenRA.Mods.Cnc/FileFormats/CRC32.cs rename to OpenRA.Game/FileFormats/CRC32.cs index 54eeb7548c..bb542b40bf 100644 --- a/OpenRA.Mods.Cnc/FileFormats/CRC32.cs +++ b/OpenRA.Game/FileFormats/CRC32.cs @@ -11,7 +11,7 @@ using System; -namespace OpenRA.Mods.Cnc.FileFormats +namespace OpenRA.FileFormats { /// /// Static class that uses a lookup table to calculates CRC32 @@ -118,5 +118,16 @@ namespace OpenRA.Mods.Cnc.FileFormats { return Calculate(data, 0xFFFFFFFF); } + + // Chaining method: accepts the current CRC state + public static uint Update(uint crc, ReadOnlySpan data) + { + for (var i = 0; i < data.Length; i++) + crc = (crc >> 8) ^ LookUp[(crc & 0xFF) ^ data[i]]; + return crc; + } + + // Standard PNG start: 0xFFFFFFFF, Finalize: XOR 0xFFFFFFFF + public static uint Finish(uint crc) => crc ^ 0xFFFFFFFF; } } diff --git a/OpenRA.Game/FileFormats/Png.cs b/OpenRA.Game/FileFormats/Png.cs index 8a394926e1..38cecd218e 100644 --- a/OpenRA.Game/FileFormats/Png.cs +++ b/OpenRA.Game/FileFormats/Png.cs @@ -15,7 +15,6 @@ using System.IO; using System.Linq; using System.Net; using System.Text; -using ICSharpCode.SharpZipLib.Checksum; using ICSharpCode.SharpZipLib.Zip.Compression; using ICSharpCode.SharpZipLib.Zip.Compression.Streams; using OpenRA.Graphics; @@ -333,10 +332,12 @@ namespace OpenRA.FileFormats var data = input.ReadAllBytes(); output.Write(data); - var crc32 = new Crc32(); - crc32.Update(typeBytes); - crc32.Update(data); - output.Write(IPAddress.NetworkToHostOrder((int)crc32.Value)); + var crc = 0xFFFFFFFF; + crc = CRC32.Update(crc, typeBytes); + crc = CRC32.Update(crc, data); + var finalCrc = CRC32.Finish(crc); + + output.Write(IPAddress.NetworkToHostOrder((int)finalCrc)); } public byte[] Save(Compression compression = Compression.BEST_COMPRESSION) diff --git a/OpenRA.Mods.Cnc/FileSystem/PackageEntry.cs b/OpenRA.Mods.Cnc/FileSystem/PackageEntry.cs index e4ff06ea20..caeca232d1 100644 --- a/OpenRA.Mods.Cnc/FileSystem/PackageEntry.cs +++ b/OpenRA.Mods.Cnc/FileSystem/PackageEntry.cs @@ -14,7 +14,7 @@ using System.Collections.Generic; using System.IO; using System.Runtime.InteropServices; using System.Text; -using OpenRA.Mods.Cnc.FileFormats; +using OpenRA.FileFormats; namespace OpenRA.Mods.Cnc.FileSystem {