private void ReadChunkCrc(PngChunk chunk, byte[] typeBuffer) { byte[] crcBuffer = new byte[4]; int numBytes = _stream.Read(crcBuffer, 0, 4); if (numBytes >= 1 && numBytes <= 3) { throw new ImageFormatException("Image stream is not valid!"); } Array.Reverse(crcBuffer); chunk.Crc = BitConverter.ToUInt32(crcBuffer, 0); Crc32 crc = new Crc32(); crc.Update(typeBuffer); crc.Update(chunk.Data); if (crc.Value != chunk.Crc) { throw new ImageFormatException("CRC Error. PNG Image chunk is corrupt!"); } }
private void WriteChunk(Stream stream, string type, byte[] data, int offset, int length) { WriteInteger(stream, length); byte[] typeArray = new byte[4]; typeArray[0] = (byte)type[0]; typeArray[1] = (byte)type[1]; typeArray[2] = (byte)type[2]; typeArray[3] = (byte)type[3]; stream.Write(typeArray, 0, 4); if (data != null) { stream.Write(data, offset, length); } Crc32 crc32 = new Crc32(); crc32.Update(typeArray); if (data != null) { crc32.Update(data, offset, length); } WriteInteger(stream, (uint)crc32.Value); }