|
| 1 | +using System.Buffers.Binary; |
| 2 | +using System.IO.Hashing; |
| 3 | + |
| 4 | +namespace DeterministicIoPackaging; |
| 5 | + |
| 6 | +static class PngNormalizer |
| 7 | +{ |
| 8 | + static readonly byte[] pngSignature = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]; |
| 9 | + static readonly byte[] idatType = "IDAT"u8.ToArray(); |
| 10 | + |
| 11 | + public static void Normalize(Stream source, Stream target) |
| 12 | + { |
| 13 | + var header = new byte[8]; |
| 14 | + source.ReadExactly(header, 0, 8); |
| 15 | + target.Write(pngSignature); |
| 16 | + |
| 17 | + using var idatData = new MemoryStream(); |
| 18 | + var flushedIdat = false; |
| 19 | + |
| 20 | + while (true) |
| 21 | + { |
| 22 | + source.ReadExactly(header, 0, 8); |
| 23 | + var chunkLength = BinaryPrimitives.ReadInt32BigEndian(header.AsSpan(0, 4)); |
| 24 | + |
| 25 | + var body = new byte[chunkLength + 4]; |
| 26 | + source.ReadExactly(body, 0, body.Length); |
| 27 | + |
| 28 | + if (ProcessChunk(header, body, chunkLength, target, idatData, ref flushedIdat)) |
| 29 | + { |
| 30 | + break; |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + public static async Task NormalizeAsync(Stream source, Stream target, Cancel cancel) |
| 36 | + { |
| 37 | + var header = new byte[8]; |
| 38 | + await source.ReadExactlyAsync(header, 0, 8, cancel); |
| 39 | + target.Write(pngSignature); |
| 40 | + |
| 41 | + using var idatData = new MemoryStream(); |
| 42 | + var flushedIdat = false; |
| 43 | + |
| 44 | + while (true) |
| 45 | + { |
| 46 | + await source.ReadExactlyAsync(header, 0, 8, cancel); |
| 47 | + var chunkLength = BinaryPrimitives.ReadInt32BigEndian(header.AsSpan(0, 4)); |
| 48 | + |
| 49 | + var body = new byte[chunkLength + 4]; |
| 50 | + await source.ReadExactlyAsync(body, 0, body.Length, cancel); |
| 51 | + |
| 52 | + if (ProcessChunk(header, body, chunkLength, target, idatData, ref flushedIdat)) |
| 53 | + { |
| 54 | + break; |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + static bool ProcessChunk(byte[] header, byte[] body, int chunkLength, Stream target, MemoryStream idatData, ref bool flushedIdat) |
| 60 | + { |
| 61 | + var isIdat = header[4] == 'I' && header[5] == 'D' && |
| 62 | + header[6] == 'A' && header[7] == 'T'; |
| 63 | + var isIend = header[4] == 'I' && header[5] == 'E' && |
| 64 | + header[6] == 'N' && header[7] == 'D'; |
| 65 | + |
| 66 | + if (isIdat) |
| 67 | + { |
| 68 | + idatData.Write(body, 0, chunkLength); |
| 69 | + } |
| 70 | + else |
| 71 | + { |
| 72 | + if (!flushedIdat && idatData.Length > 0) |
| 73 | + { |
| 74 | + flushedIdat = true; |
| 75 | + WriteNormalizedIdat(target, idatData.GetBuffer(), (int) idatData.Length); |
| 76 | + } |
| 77 | + |
| 78 | + target.Write(header); |
| 79 | + target.Write(body); |
| 80 | + } |
| 81 | + |
| 82 | + return isIend; |
| 83 | + } |
| 84 | + |
| 85 | + static void WriteNormalizedIdat(Stream target, byte[] zlibBytes, int zlibLength) |
| 86 | + { |
| 87 | + using var decompressedStream = new MemoryStream(); |
| 88 | + using (var zlibInput = new MemoryStream(zlibBytes, 0, zlibLength)) |
| 89 | + using (var zlibStream = new ZLibStream(zlibInput, CompressionMode.Decompress)) |
| 90 | + { |
| 91 | + zlibStream.CopyTo(decompressedStream); |
| 92 | + } |
| 93 | + |
| 94 | + using var compressOutput = new MemoryStream(); |
| 95 | + using (var zlibStream = new ZLibStream(compressOutput, CompressionLevel.Optimal, leaveOpen: true)) |
| 96 | + { |
| 97 | + zlibStream.Write(decompressedStream.GetBuffer(), 0, (int) decompressedStream.Length); |
| 98 | + } |
| 99 | + |
| 100 | + var length = (int) compressOutput.Length; |
| 101 | + var header = new byte[4]; |
| 102 | + BinaryPrimitives.WriteInt32BigEndian(header, length); |
| 103 | + target.Write(header); |
| 104 | + target.Write(idatType); |
| 105 | + target.Write(compressOutput.GetBuffer(), 0, length); |
| 106 | + |
| 107 | + var crc = new Crc32(); |
| 108 | + crc.Append(idatType); |
| 109 | + crc.Append(compressOutput.GetBuffer().AsSpan(0, length)); |
| 110 | + var crcBytes = new byte[4]; |
| 111 | + BinaryPrimitives.WriteUInt32BigEndian(crcBytes, crc.GetCurrentHashAsUInt32()); |
| 112 | + target.Write(crcBytes); |
| 113 | + } |
| 114 | +} |
0 commit comments