Skip to content

Commit

Permalink
Fixed bug and saved space
Browse files Browse the repository at this point in the history
Bit wise AND 0x80 which should be OR 0x80
Removed $content from container objects
  • Loading branch information
mtelvers committed Aug 7, 2020
1 parent 7b36414 commit c6ea789
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

*.swp
18 changes: 10 additions & 8 deletions ps-snmp.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,11 @@ Function DecodeBER {
$content = $berInput[$i..($i + $length - 1)]

if ($constructed) {
$ret += New-Object PSObject -Property @{class=$class; constructed=$constructed; tag=$tag; content=$content; inner=(DecodeBER $content)}
$ret += New-Object PSObject -Property @{class=$class; constructed=$true; tag=$tag; content=$null; inner=(DecodeBER $content)}
} else {
$ret += New-Object PSObject -Property @{class=$class; constructed=$constructed; tag=$tag; content=$content}
$ret += New-Object PSObject -Property @{class=$class; constructed=$false; tag=$tag; content=$content}
}
}

return ,$ret
}

Expand Down Expand Up @@ -297,7 +296,6 @@ Function EncodeBER {

$bytes = [byte[]]@()
foreach ($b in $berObj) {

$bits = (($b.class.value__ -band 0x3) -shl 6)
if ($b.constructed) {
$bits = $bits -bor 0x20
Expand Down Expand Up @@ -328,15 +326,19 @@ Function EncodeBER {
if ($content.length -lt 127) {
$bytes += $content.length
} else {
$len = UIntToByteArray $content.length
$bytes += $len.length -band 0x80
$bytes += $len
$num = $content.length
$len = [byte[]]@()
do {
$len += [byte]($num -band 0xff)
$num = $num -shr 8
} while ($num -gt 0)
$bytes += $len.length -bor 0x80
$bytes += $len[-1..-($len.length)]
}

if ($content.length -gt 0) {
$bytes += $content
}

}
return ,$bytes
}
Expand Down

0 comments on commit c6ea789

Please sign in to comment.