Пример #1
0
 /** Read a torrent from raw data */
 public static function read($data)
 {
     $pos = 1;
     $len = strlen($data);
     if ($data[0] !== 'd') {
         throw new Exception("Torrent data doesn't contain bencoding");
     }
     $meta = array();
     $hash = null;
     while ($pos < $len) {
         if ($data[$pos] === 'e') {
             $pos++;
             break;
         }
         $key = Bencode::decodeString($data, $pos);
         if ($key === 'info') {
             $start = $pos;
             $value = Bencode::decodeValue($data, $pos);
             $end = $pos;
             $hash = sha1(substr($data, $start, $end - $start), true);
         } else {
             $value = Bencode::decodeValue($data, $pos);
         }
         $meta[$key] = $value;
     }
     if (!isset($hash)) {
         throw new Exception("Torrent doesn't contain 'info' data");
     }
     return new self($hash, $meta);
 }