Created
July 30, 2021 06:47
-
-
Save XPlantefeve/391abe1de420256b4ccc3ea9edd2442d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# (very) quick and (very) dirty bencoding converter. | |
# I actually did not now what bencoding was and reverse engineered the few | |
# files I needed information about. | |
param( | |
[Parameter(Mandatory)] | |
[string[]]$Path | |
) | |
function vo ($hash) { | |
if ($hash.str -match '^i(?<int>\d+)e(?<rest>.*)') { | |
$hash.str = $Matches.rest | |
$Matches.int | |
} elseif ($hash.str -match '^(?<l>\d+):(?<rest>.*)') { | |
$hash.str = $Matches.rest.SubString($Matches.l) | |
$Matches.rest.SubString(0,$Matches.l) | |
} elseif ($hash.str -match '^l(?<rest>.*)') { | |
$hash.str = $hash.str.SubString(1) | |
while ($hash.str -and $hash.str -notmatch '^e') { | |
vo $hash | |
} | |
if ($hash.str -match '^e') { | |
$hash.str = $hash.str.SubString(1) | |
} | |
} elseif ($hash.str -match '^d(?<rest>.*)') { | |
o $hash | |
} | |
} | |
function vp ($hash) { | |
Write-Verbose ('vp: {0}' -f $hash.str) | |
if ($hash.str -match '^(?<nl>\d+):(?<rest>.*)') { | |
$name = $Matches.rest.SubString(0,$Matches.nl) | |
$hash.str = $Matches.rest.SubString($Matches.nl) | |
$value = vo $hash | |
if ($name -match '-date') { | |
$value = Get-Date -UnixTimeSeconds $value | |
} elseif ($name -match 'time-checked') { | |
$value = foreach ($v in $value) { | |
Get-Date -UnixTimeSeconds $v | |
} | |
} | |
$hash.o | Add-Member -NotePropertyMembers @{ | |
$name = $value | |
} | |
} else { | |
throw | |
} | |
} | |
function o ($hash) { | |
Write-Verbose ('o: {0}' -f $hash.str) | |
if ($hash.o) { $parent = $hash.o } | |
$hash.str = $hash.str.SubString(1) | |
$hash.o = [pscustomobject]::new() | |
While ( $hash.str -and $hash.str -notmatch '^e') { | |
vp $hash | |
} | |
if ($hash.str -match '^e') { | |
$hash.str = $hash.str.SubString(1) | |
} | |
$local = $hash.o | |
if ($parent) { $hash.o = $parent } | |
$local | |
} | |
function go ($str) { | |
$hash = @{ | |
str = $str | |
} | |
o $hash | |
} | |
foreach ($file in $Path) { | |
go ((gc $file -Raw -Encoding ascii) -replace ([char]10),'*') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment