mirror of https://github.com/BOINC/boinc.git
BEncoded integers for the Bittorrent stuff.
svn path=/trunk/boinc/; revision=12640
This commit is contained in:
parent
a37403a673
commit
ac80942579
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
/**
|
||||
* A BInteger takes care of the BEncoding and BDecoding of integers for the
|
||||
* Bittorrent features.
|
||||
* An integer starts with "i", has the value and then ends with "e".
|
||||
*/
|
||||
class BInteger {
|
||||
private function __construct(){
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the ordinary value decoded from $str
|
||||
* @throws an IllegalArgumentException in the case
|
||||
* that the string is malformed.
|
||||
*/
|
||||
public static function toInteger($str){
|
||||
if (substr($str, 0, 1)!="i" || substr($str, -1, 1)!="e") throw new IllegalArgumentException("BEncoded integer value does not start with i or end with e.");
|
||||
$value = substr($str, 1,-1);
|
||||
if (!is_numeric($value)) throw new IllegalArgumentException("BEncoded string has non-numeric length specification.");
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a BEncoded integer value
|
||||
*/
|
||||
public static function toEncoded($value){
|
||||
return "i".$value."e";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
?>
|
Loading…
Reference in New Issue