Private/Server/Get-PacketString.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
function Get-PacketString {
    <#
    .SYNOPSIS
    Get a string in a byte stream.

    .DESCRIPTION
    Get a string in a byte stream.

    .PARAMETER Stream
    Accepts BinaryReader.

    .EXAMPLE
    Get-PacketString -Stream $Stream

    Assumes that you already have a byte stream. See more detailed usage in
    Get-SteamServerInfo.

    .NOTES
    Author: Jordan Borean and Chris Dent
    #>


    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [System.IO.BinaryReader]$Stream
    )

    process {
        # Find the end of the string, terminated with \0 and convert that byte range to a string.
        $stringBytes = while ($true) {
            $byte = $Stream.ReadByte()
            if ($byte -eq 0) {
                break
            }
            $byte
        }

        if ($stringBytes.Count -gt 0) {
            [System.Text.Encoding]::UTF8.GetString($stringBytes)
        }
    } # Process
} # Cmdlet