mirror of
https://github.com/MrUnknownDE/VRCX.git
synced 2026-05-03 13:36:04 +02:00
feat: Add metadata removal functionality
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Buffers.Binary;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
@@ -117,6 +118,35 @@ public struct PNGChunk
|
||||
return new Tuple<int, int>(width, height);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates this chunk against the chunk at the same index in a given file stream, checking the chunk length and CRC.
|
||||
/// </summary>
|
||||
/// <param name="fileStream">The file stream from which the chunk data is read for validation.</param>
|
||||
/// <returns>True if chunk exists at index and is valid, false otherwise.</returns>
|
||||
public bool ExistsInFile(FileStream fileStream)
|
||||
{
|
||||
fileStream.Seek(Index, SeekOrigin.Begin);
|
||||
byte[] buffer = new byte[Length];
|
||||
fileStream.ReadExactly(buffer, 0, Length);
|
||||
|
||||
if (BitConverter.IsLittleEndian)
|
||||
Array.Reverse(buffer, 0, 4);
|
||||
|
||||
int chunkLength = BitConverter.ToInt32(buffer, 0);
|
||||
if (chunkLength != Length)
|
||||
return false;
|
||||
|
||||
fileStream.Seek(4 + chunkLength, SeekOrigin.Current);
|
||||
fileStream.ReadExactly(buffer, 0, Length);
|
||||
|
||||
if (BitConverter.IsLittleEndian)
|
||||
Array.Reverse(buffer, 0, 4);
|
||||
|
||||
uint crc = BitConverter.ToUInt32(buffer, 0);
|
||||
|
||||
return crc == CalculateCRC();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Constructs and returns a byte array representation of the PNG chunk. Generates a CRC.
|
||||
/// This data can be added to a PNG file as-is.
|
||||
@@ -146,7 +176,7 @@ public struct PNGChunk
|
||||
Buffer.BlockCopy(Data, 0, result, 8, Data.Length);
|
||||
|
||||
// Calculate and copy CRC
|
||||
uint crc = Crc32(Data, 0, Data.Length, Crc32(chunkTypeBytes, 0, chunkTypeBytes.Length, 0));
|
||||
uint crc = CalculateCRC();
|
||||
uint reversedCrc = BinaryPrimitives.ReverseEndianness(crc);
|
||||
|
||||
Buffer.BlockCopy(BitConverter.GetBytes(reversedCrc), 0, result, totalLength - 4, 4);
|
||||
@@ -154,6 +184,12 @@ public struct PNGChunk
|
||||
return result;
|
||||
}
|
||||
|
||||
public uint CalculateCRC()
|
||||
{
|
||||
var chunkTypeBytes = Encoding.ASCII.GetBytes(ChunkType);
|
||||
return Crc32(Data, 0, Data.Length, Crc32(chunkTypeBytes, 0, chunkTypeBytes.Length, 0));
|
||||
}
|
||||
|
||||
// Crc32 implementation from
|
||||
// https://web.archive.org/web/20150825201508/http://upokecenter.dreamhosters.com/articles/png-image-encoder-in-c/
|
||||
private static uint Crc32(byte[] stream, int offset, int length, uint crc)
|
||||
|
||||
Reference in New Issue
Block a user