/** * Decode an encoded PHP associative array * * @param string $dictionary * @return array * @throws PHP_BitTorrent_Decoder_Exception */ public static function decodeDictionary($dictionary) { if ($dictionary[0] !== 'd') { throw new PHP_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 = static::decodeString($keyPart); $keyPartLength = strlen(PHP_BitTorrent_Encoder::encodeString($key)); $valuePart = substr($dictionary, $i + $keyPartLength); $value = static::decode($valuePart); $valuePartLength = strlen(PHP_BitTorrent_Encoder::encode($value)); $ret[$key] = $value; $i += $keyPartLength + $valuePartLength; } return $ret; }