Skip to content

Instantly share code, notes, and snippets.

@joshooaj
Created December 3, 2024 21:30
Show Gist options
  • Save joshooaj/86c0efb601e8613b2ad704b966e68973 to your computer and use it in GitHub Desktop.
Save joshooaj/86c0efb601e8613b2ad704b966e68973 to your computer and use it in GitHub Desktop.
Parse PNG files into their individual chunks
function ParsePng {
[CmdletBinding()]
param(
[Parameter(Mandatory, Position = 0)]
[string]
$Path
)
process {
try {
$Path = (Resolve-Path -Path $Path).Path
$file = [io.file]::OpenRead($Path)
$reader = [io.binaryreader]::new($file)
Write-Host "Length: $($reader.BaseStream.Length)"
Write-Host "Position: $($reader.BaseStream.Position)"
$null = $reader.ReadBytes(8)
while ($reader.BaseStream.Position -lt $reader.BaseStream.Length) {
Write-Host "Position: $($reader.BaseStream.Position)"
$rawLength = $reader.ReadBytes(4)
[array]::Reverse($rawLength)
$length = [bitconverter]::ToUInt32($rawLength, 0)
$type = $reader.ReadBytes(4)
$data = $reader.ReadBytes($length)
$crc = $reader.ReadBytes(4)
[pscustomobject]@{
Length = $length
Type = [text.encoding]::ASCII.GetString($type)
Data = $data
CRC = $crc
}
}
} finally {
$reader.Dispose()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment