示例#1
0
 /**
  * Decode an encoded PHP associative array
  *
  * @param string $dictionary
  * @return array
  * @uses Zend_BitTorrent_Decoder::decodeString()
  * @uses Zend_BitTorrent_Decoder::decode()
  * @throws Zend_BitTorrent_Decoder_Exception
  */
 public static function decodeDictionary($dictionary)
 {
     if ($dictionary[0] !== 'd') {
         /** @see Zend_BitTorrent_Decoder_Exception */
         require_once 'Zend/BitTorrent/Decoder/Exception.php';
         throw new Zend_BitTorrent_Decoder_Exception('Parameter is not an encoded dictionary.');
     }
     $length = strlen($dictionary);
     $ret = array();
     $i = 1;
     while ($i < $length) {
         if ($dictionary[$i] === 'e') {
             break;
         }
         $keyPart = substr($dictionary, $i);
         $key = self::decodeString($keyPart);
         $keyPartLength = strlen(Zend_BitTorrent_Encoder::encodeString($key));
         $valuePart = substr($dictionary, $i + $keyPartLength);
         $value = self::decode($valuePart);
         $valuePartLength = strlen(Zend_BitTorrent_Encoder::encode($value));
         $ret[$key] = $value;
         $i += $keyPartLength + $valuePartLength;
     }
     return $ret;
 }