=strlen($s)) { return null; } switch($s[$pos]){ case 'd': $pos++; $retval=array(); while ($s[$pos]!='e'){ $key=bdec($s, $pos); $val=bdec($s, $pos); if ($key===null || $val===null) break; $retval[$key]=$val; } $retval["isDct"]=true; $pos++; return $retval; case 'l': $pos++; $retval=array(); while ($s[$pos]!='e'){ $val=bdec($s, $pos); if ($val===null) break; $retval[]=$val; } $pos++; return $retval; case 'i': $pos++; $digits=strpos($s, 'e', $pos)-$pos; // Proger_XP: changed (int) -> (float) to avoid trimming of values exceeding // signed int's max value (2147483647). $val=(float)substr($s, $pos, $digits); $pos+=$digits+1; return $val; // case "0": case "1": case "2": case "3": case "4": // case "5": case "6": case "7": case "8": case "9": default: $digits=strpos($s, ':', $pos)-$pos; if ($digits<0 || $digits >20) return null; $len=(float)substr($s, $pos, $digits); $pos+=$digits+1; $str=substr($s, $pos, $len); $pos+=$len; //echo "pos: $pos str: [$str] len: $len digits: $digits\n"; return (string)$str; } return null; } // Proger_XP: added added skipping for null values and $tempChar prefix for list/dicts. function benc(&$d, $tempChar = null){ if(is_array($d)){ $ret="l"; $isDict=!empty($d["isDct"]); if($isDict){ $ret="d"; // this is required by the specs, and BitTornado actualy chokes on unsorted dictionaries ksort($d, SORT_STRING); } foreach($d as $key=>$value) { if($isDict){ // skip the isDct element, only if it's set by us if (!bskip($key, $value, $tempChar)) { $ret .= strlen($key).":$key"; } } elseif (!is_int($key) and !is_float($key) and trim($key, '0..9') !== '') { // Proger_XP: added exception raising for non-numeric list keys. throw new Exception('Cannot bencode() a list - it contains a non-numeric key "'.$key.'".'); } if (is_string($value)) { $ret.=strlen($value).":".$value; } elseif (is_int($value) or is_float($value)){ $ret.="i${value}e"; } else { $ret.=benc ($value); } } return $ret."e"; } elseif (is_string($d)) { // fallback if we're given a single bencoded string or int return strlen($d).":".$d; } elseif (is_int($d) or is_float($d)) { return "i${d}e"; } else { return null; } } // bskip by Proger_XP. function bskip($key, &$value, $tempChar = null) { return ($key === 'isDct' and $value) or $value === null or strpos($key, $tempChar) === 0; } ?>