/// <summary> /// Calculate CRC32 for PRG and CHR fields and store it into PCKx and CCKx fields /// </summary> public void CalculateAndStoreCRCs() { foreach (var key in Fields.Keys.Where(k => k.StartsWith("PRG"))) { var num = key[3]; var crc32 = Crc32Calculator.CalculateCRC32(Fields[key]); Fields[$"PCK{num}"] = new byte[] { (byte)(crc32 & 0xFF), (byte)((crc32 >> 8) & 0xFF), (byte)((crc32 >> 16) & 0xFF), (byte)((crc32 >> 24) & 0xFF) }; } foreach (var key in Fields.Keys.Where(k => k.StartsWith("CHR"))) { var num = key[3]; var crc32 = Crc32Calculator.CalculateCRC32(Fields[key]); Fields[$"CCK{num}"] = new byte[] { (byte)(crc32 & 0xFF), (byte)((crc32 >> 8) & 0xFF), (byte)((crc32 >> 16) & 0xFF), (byte)((crc32 >> 24) & 0xFF) }; } }
/// <summary> /// Calculate overall CRC32 /// </summary> /// <returns></returns> public uint CalculateCRC32() => Crc32Calculator.CalculateCRC32( Enumerable.Concat(Fields.Where(k => k.Key.StartsWith("PRG")).OrderBy(k => k.Key).SelectMany(i => i.Value), Fields.Where(k => k.Key.StartsWith("CHR")).OrderBy(k => k.Key).SelectMany(i => i.Value)).ToArray() );