Skip to content

Instantly share code, notes, and snippets.

@XPlantefeve
Created July 30, 2021 06:47
Show Gist options
  • Save XPlantefeve/391abe1de420256b4ccc3ea9edd2442d to your computer and use it in GitHub Desktop.
Save XPlantefeve/391abe1de420256b4ccc3ea9edd2442d to your computer and use it in GitHub Desktop.
# (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