Jump to content

Decode Bencode Function - Help Please!


Recommended Posts

This is what I ended up with for now. Maybe someone will find this useful.

#include <IncludesWinHttp.au3>
#include <array.au3>
Global $sHTTPUserAgent = "BTWebClient/3100(26595)"

$INFOHASH = "ce1fc50bffb09962be8f3c49478cbeb65e2afe0f"
$EncodedInfoHash = _EncodeHash($INFOHASH)
ConsoleWrite("EncodedInfoHash: " & $EncodedInfoHash & @CRLF)
;http://torrent.ubuntu.com:6969/announce?info_hash=%9A%813%3C%1B%16%E4%A8%3C%10%F3%05%2C%15%90%AA%DF%5E.%20&peer_id=ABCDEFGHIJKLMNOPQRST&port=6881&uploaded=0&downloaded=0&left=727955456&event=started&numwant=100&no_peer_id=1&compact=1
$TrackerResponse = _WinHttpGet('www.h33t.com', '/announce?info_hash=' & $EncodedInfoHash & '&peer_id=ABCDEFGHIJKLMNOPQRST&port=6881&uploaded=0&downloaded=0&left=727955456&event=started&numwant=100&no_peer_id=1&compact=1')
;$TrackerResponse = _WinHttpGet('www.h33t.com', '/file?info_hash=' & $EncodedInfoHash & '&peer_id=ABCDEFGHIJKLMNOPQRST&port=6881&uploaded=0&downloaded=0&left=727955456&event=started&numwant=100&no_peer_id=1&compact=1')
;ConsoleWrite("TrackerResponse: " & $TrackerResponse & @CRLF)
$avDecoded_Tracker_Response = _Decode_Bencode($TrackerResponse)
;_ArrayDisplay($avDecoded_Tracker_Response)
$aPeers = _Get_Decoded_Data($avDecoded_Tracker_Response, 'peers')
_ArrayDisplay($aPeers)
;MsgBox(0,"", BinaryToString($aPeers[0]))
;FileWriteLine(@DesktopDir & "Peers.txt", $aPeers[0])

Func _EncodeHash($sString)
    If (Not IsString($sString)) Or $sString = "" Then Return SetError(1, 0, 0)
    Local $aArray = StringRegExp($sString, "(?s).{1," & 2 & "}", 3), $sEncodedHash
    For $i = 0 To UBound($aArray) - 1
        If StringInStr("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_~", Chr(Dec($aArray[$i]))) Then
            $sEncodedHash &= Chr(Dec($aArray[$i]))
        Else
            $sEncodedHash &= "%" & $aArray[$i]
        EndIf
    Next
    Return $sEncodedHash
EndFunc   ;==>_EncodeHash

Func _WinHttpGet($sDomainName, $sURLParam, $sReferer = Default)
    Local $hWinHttpOpen = _WinHttpOpen($sHTTPUserAgent)
    HttpSetUserAgent($sHTTPUserAgent)
    Local $hWinHttpConnect = _WinHttpConnect($hWinHttpOpen, $sDomainName, 3310)
    Local $hWinHttpRequest = _WinHttpOpenRequest($hWinHttpConnect, "GET", $sURLParam, Default, $sReferer)
    _WinHttpSendRequest($hWinHttpRequest)
    _WinHttpReceiveResponse($hWinHttpRequest)
    Local $sDownload_ResponseData, $sDownload_Chunk
    While 1
        $sDownload_Chunk = _WinHttpReadData($hWinHttpRequest)
        If @error Then ExitLoop
        $sDownload_ResponseData &= $sDownload_Chunk
    WEnd
    _WinHttpCloseHandle($hWinHttpRequest)
    Return $sDownload_ResponseData
EndFunc   ;==>_WinHttpGet

Func _Decode_Bencode($sBencoded_Data)
    Local $iCharacter_Position = 1, $iDecoded_Index = 0, $iCharacter_Position, $iEnd_Position, $iDecoded_Integer, $vDelimiter, $iInitial_Array_Size = 100, $iExpand_Array = 50
    If Not IsString($sBencoded_Data) Then Return SetError(1, 0, 0)
    Local $aDecoded_Array[$iInitial_Array_Size]
    While $iCharacter_Position <> StringLen($sBencoded_Data)
        $vDelimiter = StringMid($sBencoded_Data, $iCharacter_Position, 1)
        Select
            Case $vDelimiter = "i"
                $iEnd_Position = StringInStr($sBencoded_Data, "e", 0, 1, $iCharacter_Position) - 1
                $aDecoded_Array[$iDecoded_Index] = StringMid($sBencoded_Data, $iCharacter_Position + 1, $iEnd_Position - $iCharacter_Position)
                $iCharacter_Position = $iEnd_Position
                $iDecoded_Index += 1
            Case StringRegExp($vDelimiter, 'd')
                $iEnd_Position = StringInStr($sBencoded_Data, ":", 0, 1, $iCharacter_Position)
                $iDecoded_Integer = StringMid($sBencoded_Data, $iCharacter_Position, $iEnd_Position - $iCharacter_Position)
                $aDecoded_Array[$iDecoded_Index] = StringMid($sBencoded_Data, $iEnd_Position + 1, $iDecoded_Integer)
                $iCharacter_Position = $iEnd_Position + $iDecoded_Integer
                $iDecoded_Index += 1
        EndSelect
        $iCharacter_Position += 1
        If $iDecoded_Index >= $iInitial_Array_Size Then
            $iInitial_Array_Size += $iExpand_Array
            ReDim $aDecoded_Array[$iInitial_Array_Size]
        EndIf
    WEnd
    $aDecoded_Array[0] = $iDecoded_Index - 1
    ReDim $aDecoded_Array[$iDecoded_Index]
    Return $aDecoded_Array
EndFunc   ;==>_Decode_Bencode

Func _Get_Decoded_Data($avDecoded_Bencode, $vStart, $vEnd = "", $vAltEnd = "", $iStartIndex = 0)
    If Not IsArray($avDecoded_Bencode) Then Return SetError(1, 0, 0)
    Local $iStart = _ArraySearch($avDecoded_Bencode, $vStart, $iStartIndex) + 1
    If @error Then Return SetError(2, 0, 0)
    If $vEnd <> "" Then
        Local $iEnd = _ArraySearch($avDecoded_Bencode, $vEnd, $iStart) - 1
        If @error Then
            $iEnd = _ArraySearch($avDecoded_Bencode, $vAltEnd, $iStart) - 1
            If @error Then Return SetError(3, 0, 0)
        EndIf
    Else
        Local $iEnd = UBound($avDecoded_Bencode, 1) - 1
    EndIf
    Local $aDecoded_Array[$iEnd - $iStart + 1]
    Local $iDecoded_Index = 0
    For $i = $iStart To $iEnd Step 1
        $aDecoded_Array[$iDecoded_Index] = $avDecoded_Bencode[$i]
        $iDecoded_Index += 1
    Next
    Return $aDecoded_Array
EndFunc   ;==>_Get_Decoded_Data
Edited by Decipher
Spoiler

censored.jpg

 

Link to comment
Share on other sites

  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...