Skip to content

Instantly share code, notes, and snippets.

@lowleveldesign
Created October 27, 2022 08:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lowleveldesign/32a322b2d8162ec455853c0003b9772f to your computer and use it in GitHub Desktop.
Save lowleveldesign/32a322b2d8162ec455853c0003b9772f to your computer and use it in GitHub Desktop.
A script coloring the content of a binary file
param (
[Parameter(Position = 1, Mandatory = $True)]
[ValidateScript({ Test-Path $_ })]
[String]$Path,
[Int64]$Offset = 0
)
$ErrorActionPreference = "Stop"
$Colors = @(
[ConsoleColor]::Black,
[ConsoleColor]::White,
[ConsoleColor]::Blue,
[ConsoleColor]::Green
)
if ($Host.UI.RawUI.WindowSize.Width -lt 20 -or $Host.UI.RawUI.WindowSize.Height -lt 10) {
Write-Error "The console window is too small."
}
$ColorsPerByte = 4
$rawUI = $Host.UI.RawUI
$maxWidth = $rawUI.WindowSize.Width - ($rawUI.WindowSize.Width % $ColorsPerByte)
function Write-EncodedBytes {
param(
[byte[]]$data,
[int]$start,
[int]$cnt)
for ($i = 0; $i -lt $cnt; $i++) {
$b = $data[$start + $i]
$rawUI.BackgroundColor = $Colors[(($b -band 0xC0) -shr 6)]
Write-Host -NoNewline ' '
$rawUI.BackgroundColor = $Colors[(($b -band 0x30) -shr 4)]
Write-Host -NoNewline ' '
$rawUI.BackgroundColor = $Colors[(($b -band 0xC) -shr 2)]
Write-Host -NoNewline ' '
$rawUI.BackgroundColor = $Colors[($b -band 0x3)]
Write-Host -NoNewline ' '
}
}
function Write-Header {
param ([int]$FileLength)
Clear-Host
$availConsoleLines = $rawUI.WindowSize.Height - 1
# intial header lines
$rawUI.BackgroundColor = [ConsoleColor]::Green
1..4 | Foreach-Object {
1..($maxWidth) | Foreach-Object { Write-Host -NoNewline ' ' }
Write-Host
$availConsoleLines--
}
# draw all possible colors
$Colors | ForEach-Object {
$rawUI.BackgroundColor = $_;
Write-Host -NoNewline ' '
}
Write-Host; $availConsoleLines--
# encode screen width and height
$b = [BitConverter]::GetBytes($maxWidth)
Write-EncodedBytes $b 0 $b.Length
$b = [BitConverter]::GetBytes($rawUI.WindowSize.Height)
Write-EncodedBytes $b 0 $b.Length
Write-Host; $availConsoleLines--
# write line of colors for validation
$b = @(0x1b) # 00011011
1..([int]($maxWidth / $ColorsPerByte)) | ForEach-Object { Write-EncodedBytes $b 0 1 }
Write-Host; $availConsoleLines--
# encode file length
$b = [BitConverter]::GetBytes($FileLength)
Write-EncodedBytes $b 0 $b.Length
Write-Host; $availConsoleLines--
# fill the rest of the console screen
while ($availConsoleLines -gt 0) {
Write-Host; $availConsoleLines--
}
}
$md5 = [System.Security.Cryptography.MD5]::Create()
function Write-Hash {
param (
[byte[]]$data,
[int]$cnt
)
$hash = $md5.ComputeHash($data, 0, $cnt)
Write-EncodedBytes $hash 0 $hash.Length
Write-Host
}
function Write-ScreenEnding {
$rawUI.BackgroundColor = $Colors[$script:lastEndingColor]
1..($rawUI.WindowSize.Width - 1) | Foreach-Object { Write-Host -NoNewline ' ' }
$resp = Read-Host
$script:lastEndingColor = ($script:lastEndingColor + 1) % $Colors.Length
$resp -eq "n"
}
$prevBackground = $rawUI.BackgroundColor
$Path = Resolve-Path $Path
$f = [IO.File]::OpenRead($Path)
$f.Seek($Offset, [System.IO.SeekOrigin]::Begin) | Out-Null
$lastEndingColor = 0
Write-Header $f.Length
Write-ScreenEnding | Out-Null
$windowSize = $rawUI.WindowSize
$data = New-Object byte[] ([int]($maxWidth * ($windowSize.Height - 2) / $ColorsPerByte))
$linelen = [int]($maxWidth / $ColorsPerByte)
$cnt = $data.Length
while ($cnt -eq $data.Length) {
$cnt = $f.Read($data, 0, $data.Length)
do {
$bytesleft = $cnt
for ($line = 0; $line -lt $windowSize.Height - 2; $line++) {
if ($bytesleft -gt 0) {
Write-EncodedBytes $data ($line * $linelen) ([Math]::Min($linelen, $bytesleft))
$bytesleft -= $linelen
}
Write-Host
}
Write-Hash $data $cnt
} while (-not (Write-ScreenEnding))
}
$f.Dispose()
$rawUI.BackgroundColor = $prevBackground
Get-FileHash $Path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment