Jump to content

Torrent (bencode) UDF


LoranRendel
 Share

Recommended Posts

Hello,

This is my UDF working with *.torrent files.

It converts torrent file to readable and editable text structure and back to torrent.

Binary data will be encoded in base64.

Written on pure AutoIt, except base64 located

Due to limitation of this version of base64 code, code will not work on x64 compilation.

Updated to new x64 compatible code.

This code can also be used to edit utorrent/BitTorrent config files (like settings.dat), as it encoded using bencode.

Examples are included.

torrentUDF.zip

Edited by xrewndel
Link to comment
Share on other sites

  • 11 months later...

Thanks for making this, I was having trouble working with torrent files in autoit until I found this thread.

I modified your _Torrent_Parse function slightly to add outputting of the infohash for a torrent file because I had a need to do this:

#include <Crypt.au3>

Func _Torrent_Parse($File)
    Local $Data, $Binary, $hFile, $Byte, $Number, $String, $Size, $infodictlevel, $recording_info_bytes=false, $infobytes
    $hFile = FileOpen($File, 16)
    If @error Then Return SetError(1, 0, '')
    While True
        $Byte = BinaryToString(FileRead($hFile, 1))
        if $recording_info_bytes then $infobytes &= $Byte
        If @error Then ExitLoop
        Switch $Byte
            Case 'd'; Dictionary ->
                $Data &= __Torrents_StringFormat('Dictionary')
                __Torrents_Level(1)
                if $string='info' then
                    $infodictlevel=__Torrents_Level()
                    $recording_info_bytes = true
                    $infobytes = $Byte
                EndIf
            Case 'l'; List ->
                $Data &= __Torrents_StringFormat('List')
                __Torrents_Level(1)
            Case 'i'; Integer
                $Number = ''
                Do
                    $Number &= $Byte
                    $Byte = BinaryToString(FileRead($hFile, 1))
                    if $recording_info_bytes then $infobytes &= $Byte
                    If @error Then ExitLoop
                Until $Byte = 'e'
                VarTrim($Number, 1)
                Switch $String
                    Case 'length', 'piece length'
                        If $Number < 2 ^ 20 Then
                            $Number &= ' (' & Round($Number / 2 ^ 10, 3) & ' KiB)'
                        ElseIf $Number >= 2 ^ 20 And $Number < 2 ^ 30 Then
                            $Number &= ' (' & Round($Number / 2 ^ 20, 3) & ' MiB)'
                        Else
                            $Number &= ' (' & Round($Number / 2 ^ 30, 3) & ' GiB)'
                        EndIf
                    Case 'creation date'
                        $Number &= ' (' & _DateAdd('s', $Number, "1970/01/01 00:00:00") & ')'
                EndSwitch
                $Data &= __Torrents_StringFormat('integer: ' & $Number)
            Case 'e'; End of element <-
                __Torrents_Level(-1)
                if __Torrents_Level()=$infodictlevel-1 then $recording_info_bytes = false
                $Data &= __Torrents_StringFormat('End')
            Case '0' To '9'; String's size
                $Size &= $Byte
            Case ':'; String
                $String = BinaryToString(FileRead($hFile, $Size))
                if $recording_info_bytes then $infobytes &= $String
                $Size = ''
                If StringInStr($String, @LF) or StringInStr($String, @CR) Then
                    $Data &= __Torrents_StringFormat('binary: ' & _Base64Encode(StringToBinary($String), 0))
                Else
                    $Data &= __Torrents_StringFormat('string: ' & $String)
                EndIf
            Case Else; Error
                Return SetError(2, 0, '')
        EndSwitch
    WEnd
    Local $bHash = _Crypt_HashData($infobytes, $CALG_SHA1)
    $Data &= @CRLF& 'Infohash: '&StringTrimLeft($bHash,2)
    Return StringAddCR($Data)
EndFunc   ;==>_Torrent_Parse
Edited by garbb
Link to comment
Share on other sites

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...