Solved

extracting information from .torrent files

Posted on 2004-04-10
7
2,510 Views
Last Modified: 2012-05-04
I would like to use PHP to extract information from .torrent files, which are created according the BitTorrent protocol. Upon opening a .torrent file with a text editor, I obse4rved that the address of the relevent tracker is the first thing to appear in such files. The rest of the file's contents were in an encoding that could not be understood by viewing the file in a text editor. However, I'm pretty sure that .torrent files contain a list of the shared folder and file names, in addition to the hash code of each file. Is there a PHP library that could assist me in extracting these folder and file names? If not, how can a PHP script get access to the file names that are stored within a .torrent file?  Thank you.
0
Comment
Question by:bobwood2000
  • 3
  • 2
  • 2
7 Comments
 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10799224
There is no such library in php.
0
 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10799236
First of all why do you want to use php to do something like this.
Such things is supposed to be done in non-scripting languages like C/C++ java...

I dont want to discourage you, but think of your language choice..

You have to read the file in binary mode using fread() and manipulate data accordingly.

http://us2.php.net/fread
0
 

Author Comment

by:bobwood2000
ID: 10799312
I was hoping to use PHP to extract this information from a .torrent file so that I could keep my entire web site in a single language. Since there is no such library (thank you, venkateshwarr), I would like to know what language I should use to extract this data from the torrent file. I suppose that I could have PHP call the program that is written in this other language. I would now like to know if there exists a library (in any language) that makes it easy to understand the data in a torrent file?

Thank you.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10799402
Did you try for any open source codes which you can use...
0
 
LVL 5

Accepted Solution

by:
TheClickMaster earned 500 total points
ID: 10799791
I wrote this. I hope I will get the points since it took me over 2 hours =)

This will parse a torrent file and display the array. You may use this to
create whatever you want to do with it.

I hope you can use it and I havent wasted 2 hours of my life although it was
very interesting to program. It has a recursive function that explores the torrent file.

To see it working have a look at: http://theclickmaster.com/extract.php

There is still a bug with the "pieces" since they should all have a lenght of 20 but it definitely doesnt work right. Some kind of binary encoding ... If anyone knows what it is contact me ;)


<?php

//==============================
// Torrent Parser by mYsTic aka TheClickMaster
// 11-april-2004
//
// Contact: wi_dark@hotmail.com
//
// For more information on the bittorrent
// specifications go to:
//
// http://bitconjurer.org/BitTorrent/protocol.html
//-----------------------------------------------

// open a file
// >>>>>> customize this
$file = fopen("./ap.torrent","r");
//-----------------------------------------------

// read it
$contents = fread($file, filesize("./x.torrent"));

// close file
fclose($file);

// decode the data. torrent stuff is utf8 encoded
$contents = utf8_decode($contents);

// start the whole process.
explore($contents,&$Data,"root");

// splits the data from a pieces element into individual
// strings of length 20, each of which is the SHA1 hash
// of the piece at the corresponding index.
function get_pieces($content,&$Data)
{
      //$content = bin2hex($content);
      // get the lenght of the data
      $lenght = strlen($content);
      // calculate number of pieces
      $nb_pieces = $lenght / 20;
      // go throught the string and separate it into pieces
      for ($Cpt = 0; $Cpt < $nb_pieces; $Cpt ++)
      {
            // get a substring
            $Data["Piece " + strval($Cpt)] = substr($content,$Cpt * 20,20);
      }
}

// explore the data from a torrent file
function explore($content,&$Data,$parent_name)
 {
      // current position
      $Cpt = 0;
      
      //sub array counter
      $sub_arr = 0;
      
      // buffer, containts the lenght for the next text field
      $buffer = "";
      // name of the current dictionnary element
      $iname = "";
      
      // while we are not at the end of the data
      // and we are not at the end of the current element
      while($Cpt < strlen($content) && $content[$Cpt] != "e")
      {
            // if we have a dictionnary, list or integer
            if($content[$Cpt] == "d" || $content[$Cpt] == "l" || $content[$Cpt] == "i")
            {
                  // If this item has no name
                  if($iname == "")
                  {
                        // create a generic name based on the parents name and
                        // a unique number
                        $iname = $parent_name."-".$sub_arr;
                        // increase the number
                        $sub_arr ++;
                  }
                                    
                  // call the function recursively to parse this item
                  // the function will also return a number that tells
                  // us how far we have to move in the data and we
                  // increase $Cpt in consequence
                  $Cpt += explore(substr($content,$Cpt+1),&$Data[$iname],$iname);

                  // clear our item name
                  $iname = "";
            }
            else
            {
                  // if we have the begining of a string
                  if ($content[$Cpt] == ":")
                  {
                        // get the lenght of the string from the buffer
                        $lenght = intval($buffer);
                        
                        // if we do not have an element name saved
                        if ($iname == "")
                        {
                              // means this string is the name of an element
                              // set the element name to the substring based on the lenght
                              $iname = substr($content,$Cpt+1,$lenght);
                              // clear our lenght buffer
                              $buffer = "";
                        }
                        else
                        {
                              // we alrady have a name so this must be a string element
                              // if this is the "pieces" element
                              if ($iname == "pieces")
                              {
                                    // decompose the data
                                    $Dat[$iname] = get_pieces(substr($content,$Cpt+1,$lenght),&$Data[$iname]);
                              }                        
                              else
                              {
                                    // get the string and save it with the current element name
                                    $Data[$iname] = substr($content,$Cpt+1,$lenght);
                              }
                              // clear the buffer and the element name
                              $iname = "";
                              $buffer = "";
                        }
                        // move further in the data
                        // we skip the equivalent of the lenght of this string
                        $Cpt += $lenght;
                  }
                  else
                  {
                        // it is a number that will tell us the lenght of the next string
                        $buffer .= $content[$Cpt];      
                  }
            }
            // move 1 character
            $Cpt ++;
      }
      // if we still have data in $buffer
      // that means this was an Integer element
      // so we save the buffer's value in the current array element
      if ($buffer != "") $Data = intval($buffer);

      // return the lenght of what we have explored
      return $Cpt + 1;
 }

// dump the array using <pre> to make it pretty
echo "<pre>";
var_dump($Data);
echo "</pre>";

// thats all =)
?>
0
 

Author Comment

by:bobwood2000
ID: 10800157
Wow! Thank you very much.
0
 
LVL 5

Expert Comment

by:TheClickMaster
ID: 10800978
No problem I enjoyed doing it

Power of PHP ;)

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now