57 lines
2.7 KiB
PowerShell
57 lines
2.7 KiB
PowerShell
# Точная подгонка блока Ventoy по байтам: вернуть размер файла 325669.
|
|
$ErrorActionPreference = 'Stop'
|
|
$f = 'F:\efi\Paravis\Boot\g4d\grldr'
|
|
$origSize = 325669
|
|
$b = [IO.File]::ReadAllBytes($f)
|
|
|
|
function Find-Bytes([byte[]]$hay, [byte[]]$needle, [int]$from = 0) {
|
|
for ($i = $from; $i -le $hay.Length - $needle.Length; $i++) {
|
|
$ok = $true
|
|
for ($j = 0; $j -lt $needle.Length; $j++) { if ($hay[$i+$j] -ne $needle[$j]) { $ok = $false; break } }
|
|
if ($ok) { return $i }
|
|
}
|
|
return -1
|
|
}
|
|
|
|
$start = Find-Bytes $b ([Text.Encoding]::ASCII.GetBytes('title'))
|
|
while ($start -ge 0) {
|
|
$chunk = [Text.Encoding]::UTF8.GetString($b[$start..([Math]::Min($b.Length-1, $start+40))])
|
|
if ($chunk -match '^title\s+Ventoy \(ISO') { break }
|
|
$start = Find-Bytes $b ([Text.Encoding]::ASCII.GetBytes('title')) ($start + 5)
|
|
}
|
|
if ($start -lt 0) { throw 'Ventoy-блок не найден' }
|
|
$nextTitle = Find-Bytes $b ([Text.Encoding]::ASCII.GetBytes("`ntitle")) $start
|
|
if ($nextTitle -lt 0) { throw 'следующий title не найден' }
|
|
$blockEnd = $nextTitle + 1
|
|
|
|
$needShrink = $b.Length - $origSize
|
|
"--- файл сейчас {0} байт, нужно вернуть {1} (убрать {2})" -f $b.Length, $origSize, $needShrink
|
|
|
|
$line1 = 'title' + (' ' * 13) + 'Ventoy (ISO образы)'
|
|
$line2 = 'chainloader (bd)/efi/Paravis/boot/g4d/Ventoy'
|
|
$core = [Text.Encoding]::UTF8.GetBytes($line1 + "`n" + $line2 + "`n")
|
|
$target = ($blockEnd - $start) - $needShrink
|
|
if ($core.Length -gt $target) { throw ("не влезает: {0} > {1}" -f $core.Length, $target) }
|
|
$pad = New-Object byte[] ($target - $core.Length)
|
|
for ($i = 0; $i -lt $pad.Length; $i++) { $pad[$i] = 0x20 }
|
|
|
|
# добивка ставится в конец строки chainloader (перед последним \n)
|
|
$body = New-Object System.Collections.Generic.List[byte]
|
|
$body.AddRange([byte[]]$core[0..($core.Length-2)])
|
|
$body.AddRange($pad)
|
|
$body.Add(0x0A)
|
|
|
|
if (Get-Process -Name vmware-vmx -ErrorAction SilentlyContinue) { throw 'VM запущена — отмена' }
|
|
|
|
$out = New-Object System.Collections.Generic.List[byte]
|
|
$out.AddRange([byte[]]$b[0..($start-1)])
|
|
$out.AddRange($body.ToArray())
|
|
$out.AddRange([byte[]]$b[$blockEnd..($b.Length-1)])
|
|
[IO.File]::WriteAllBytes($f, $out.ToArray())
|
|
|
|
$nb = [IO.File]::ReadAllBytes($f)
|
|
"--- файл: {0} байт (цель {1}) {2}" -f $nb.Length, $origSize, $(if ($nb.Length -eq $origSize) { 'OK' } else { 'ПРОВЕРИТЬ' })
|
|
'--- блок:'
|
|
$t = [Text.Encoding]::UTF8.GetString($nb[$start..($start + $target - 1)])
|
|
($t -split "`n") | ForEach-Object { ' |' + (($_ -replace ' ','·').Substring(0,[Math]::Min(60,$_.Length))) }
|