Bencode.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. <?php
  2. /**
  3. * Rych Bencode Component
  4. *
  5. * @package Rych\Bencode
  6. * @author Ryan Chouinard <rchouinard@gmail.com>
  7. * @copyright Copyright (c) 2014, Ryan Chouinard
  8. * @license MIT License - http://www.opensource.org/licenses/mit-license.php
  9. */
  10. class Bencode
  11. {
  12. const TYPE_ARRAY = 'array';
  13. const TYPE_OBJECT = 'object'; // NOT IMPLEMENTED
  14. /**
  15. * Decodes a bencoded string
  16. *
  17. * @param string $string The bencoded string to decode.
  18. * @param string $decodeType Flag used to indicate whether the decoded
  19. * value should be returned as an object or an array.
  20. * @return mixed Returns the appropriate data type for the bencoded data.
  21. */
  22. public static function decode($string, $decodeType = self::TYPE_ARRAY)
  23. {
  24. return Decoder::decode($string, $decodeType);
  25. }
  26. /**
  27. * Encodes a value into a bencoded string
  28. *
  29. * @param mixed $value The value to bencode.
  30. * @return string Returns a bencoded string.
  31. */
  32. public static function encode($value)
  33. {
  34. return Encoder::encode($value);
  35. }
  36. }
  37. class Decoder
  38. {
  39. /**
  40. * @var string
  41. */
  42. private $_source;
  43. /**
  44. * @var string
  45. */
  46. private $_decodeType;
  47. /**
  48. * @var integer
  49. */
  50. private $_sourceLength;
  51. /**
  52. * @var integer
  53. */
  54. private $_offset = 0;
  55. /**
  56. * Class constructor
  57. *
  58. * @param string $source The bencode string to be decoded.
  59. * @param string $decodeType currently unused.
  60. * @return void
  61. */
  62. private function __construct($source, $decodeType)
  63. {
  64. $this->_source = $source;
  65. $this->_sourceLength = strlen($this->_source);
  66. if ($decodeType != Bencode::TYPE_ARRAY && $decodeType != Bencode::TYPE_OBJECT) {
  67. $decodeType = Bencode::TYPE_ARRAY;
  68. }
  69. $this->_decodeType = $decodeType;
  70. }
  71. /**
  72. * Decode a bencode entity into a value
  73. *
  74. * @param string $source The bencode string to be decoded.
  75. * @param string $decodeType currently unused.
  76. * @return mixed Returns the decoded value.
  77. * @throws Rych\Bencode\Exception\RuntimeException
  78. */
  79. static public function decode($source, $decodeType = Bencode::TYPE_ARRAY)
  80. {
  81. if (!is_string($source)) {
  82. throw new RuntimeException('Argument expected to be a string; Got ' . gettype($source));
  83. }
  84. $decoder = new self($source, $decodeType);
  85. $decoded = $decoder->_decode();
  86. if ($decoder->_offset != $decoder->_sourceLength) {
  87. throw new RuntimeException(
  88. 'Found multiple entities outside list or dict definitions'
  89. );
  90. }
  91. return $decoded;
  92. }
  93. /**
  94. * Decode a bencode entity into a value
  95. *
  96. * @return mixed Returns the decoded value.
  97. * @throws Rych\Bencode\Exception\RuntimeException
  98. */
  99. private function _decode()
  100. {
  101. switch ($this->_getChar()) {
  102. case 'i':
  103. ++$this->_offset;
  104. return $this->_decodeInteger();
  105. break;
  106. case 'l':
  107. ++$this->_offset;
  108. return $this->_decodeList();
  109. break;
  110. case 'd':
  111. ++$this->_offset;
  112. return $this->_decodeDict();
  113. break;
  114. default:
  115. if (ctype_digit($this->_getChar())) {
  116. return $this->_decodeString();
  117. }
  118. }
  119. throw new RuntimeException('Unknown entity found at offset ' . $this->_offset);
  120. }
  121. /**
  122. * Decode a bencode integer into an integer
  123. *
  124. * @return integer Returns the decoded integer.
  125. * @throws Rych\Bencode\Exception\RuntimeException
  126. */
  127. private function _decodeInteger()
  128. {
  129. $offsetOfE = strpos($this->_source, 'e', $this->_offset);
  130. if (false === $offsetOfE) {
  131. throw new RuntimeException('Unterminated integer entity at offset ' . $this->_offset);
  132. }
  133. $currentOffset = $this->_offset;
  134. if ('-' == $this->_getChar($currentOffset)) {
  135. ++$currentOffset;
  136. }
  137. /* if ('-' == $this->_getChar($currentOffset) && '0' == $this->_getChar($currentOffset + 1)) {
  138. throw new RuntimeException('Illegal zero-padding found in integer entity at offset ' . $this->_offset);
  139. }*/
  140. if ($offsetOfE === $currentOffset) {
  141. throw new RuntimeException('Empty integer entity at offset ' . $this->_offset);
  142. }
  143. while ($currentOffset < $offsetOfE) {
  144. if (!ctype_digit($this->_getChar($currentOffset))) {
  145. throw new RuntimeException('Non-numeric character found in integer entity at offset ' . $this->_offset);
  146. }
  147. ++$currentOffset;
  148. }
  149. $value = substr($this->_source, $this->_offset, $offsetOfE - $this->_offset);
  150. // Cjdns pads zeros which is a CLEAR violation of the bencode standard,
  151. // so we have to adjust the validation.
  152. // $absoluteValue = (string) abs($value);
  153. //if (1 < strlen($absoluteValue) && '0' == $value[0]) {
  154. if (1 < strlen($value) && '0' == $value[0]) {
  155. // TODO: Could probably just trigger a warning here
  156. throw new RuntimeException('Illegal zero-padding found in integer entity at offset ' . $this->_offset);
  157. }
  158. $this->_offset = $offsetOfE + 1;
  159. // The +0 auto-casts the chunk to either an integer or a float(in cases
  160. // where an integer would overrun the max limits of integer types)
  161. return $value + 0;
  162. }
  163. /**
  164. * Decode a bencode string into a string
  165. *
  166. * @return string Returns the decoded string.
  167. * @throws Rych\Bencode\Exception\RuntimeException
  168. */
  169. private function _decodeString()
  170. {
  171. /* if ('0' === $this->_getChar() && ':' != $this->_getChar($this->_offset + 1)) {
  172. // TODO: Trigger a warning instead?
  173. throw new RuntimeException('Illegal zero-padding in string entity length declaration at offset ' . $this->_offset);
  174. }*/
  175. $offsetOfColon = strpos($this->_source, ':', $this->_offset);
  176. if (false === $offsetOfColon) {
  177. throw new RuntimeException('Unterminated string entity at offset ' . $this->_offset);
  178. }
  179. $contentLength = (int) substr($this->_source, $this->_offset, $offsetOfColon);
  180. if (($contentLength + $offsetOfColon + 1) > $this->_sourceLength) {
  181. throw new RuntimeException('Unexpected end of string entity at offset ' . $this->_offset);
  182. }
  183. $value = substr($this->_source, $offsetOfColon + 1, $contentLength);
  184. $this->_offset = $offsetOfColon + $contentLength + 1;
  185. return $value;
  186. }
  187. /**
  188. * Decode a bencode list into a numeric array
  189. *
  190. * @return array Returns the decoded array.
  191. * @throws Rych\Bencode\Exception\RuntimeException
  192. */
  193. private function _decodeList()
  194. {
  195. $list = array ();
  196. $terminated = false;
  197. $listOffset = $this->_offset;
  198. while (false !== $this->_getChar()) {
  199. if ('e' == $this->_getChar()) {
  200. $terminated = true;
  201. break;
  202. }
  203. $list[] = $this->_decode();
  204. }
  205. if (!$terminated && false === $this->_getChar()) {
  206. throw new RuntimeException('Unterminated list definition at offset ' . $listOffset);
  207. }
  208. ++$this->_offset;
  209. return $list;
  210. }
  211. /**
  212. * Decode a bencode dictionary into an associative array
  213. *
  214. * @return array Returns the decoded array.
  215. * @throws Rych\Bencode\Exception\RuntimeException
  216. */
  217. private function _decodeDict()
  218. {
  219. $dict = array ();
  220. $terminated = false;
  221. $dictOffset = $this->_offset;
  222. while (false !== $this->_getChar()) {
  223. if ('e' == $this->_getChar()) {
  224. $terminated = true;
  225. break;
  226. }
  227. $keyOffset = $this->_offset;
  228. if (!ctype_digit($this->_getChar())) {
  229. throw new RuntimeException('Invalid dictionary key at offset ' . $keyOffset);
  230. }
  231. $key = $this->_decodeString();
  232. if (isset ($dict[$key])) {
  233. // TODO: This could probably just trigger a warning...
  234. throw new RuntimeException('Duplicate dictionary key at offset ' . $keyOffset);
  235. }
  236. $dict[$key] = $this->_decode();
  237. }
  238. if (!$terminated && false === $this->_getChar()) {
  239. throw new RuntimeException('Unterminated dictionary definition at offset ' . $dictOffset);
  240. }
  241. ++$this->_offset;
  242. return $dict;
  243. }
  244. /**
  245. * Fetch the character at the specified source offset
  246. *
  247. * If not offset is provided, the current offset is used.
  248. *
  249. * @param integer $offset the offset to retrieve from the source string.
  250. * @return string Returns the character found at the specified offset. If
  251. * the specified offset is out of range, false is returned.
  252. */
  253. private function _getChar($offset = null)
  254. {
  255. if (null === $offset) {
  256. $offset = $this->_offset;
  257. }
  258. if (empty ($this->_source) || $this->_offset >= $this->_sourceLength) {
  259. return false;
  260. }
  261. return $this->_source[$offset];
  262. }
  263. }
  264. class Encoder
  265. {
  266. /**
  267. * @var mixed Entity to be encoded.
  268. */
  269. private $_data;
  270. /**
  271. * Class constructor
  272. *
  273. * @param mixed $data Entity to be encoded.
  274. * @return void
  275. */
  276. private function __construct($data)
  277. {
  278. $this->_data = $data;
  279. }
  280. /**
  281. * Encode a value into a bencode entity
  282. *
  283. * @param mixed $data The value to be encoded.
  284. * @return string Returns the bencoded entity.
  285. */
  286. static public function encode($data)
  287. {
  288. if (is_object($data)) {
  289. if (method_exists($data, 'toArray')) {
  290. $data = $data->toArray();
  291. } else {
  292. $data = (array) $data;
  293. }
  294. }
  295. $encoder = new self($data);
  296. return $encoder->_encode();
  297. }
  298. /**
  299. * Encode a value into a bencode entity
  300. *
  301. * @param mixed $data The value to be encoded.
  302. * @return string Returns the bencoded entity.
  303. */
  304. private function _encode($data = null)
  305. {
  306. $data = is_null($data) ? $this->_data : $data;
  307. if (is_array($data) && (isset ($data[0]) || empty ($data))) {
  308. return $this->_encodeList($data);
  309. } else if (is_array($data)) {
  310. return $this->_encodeDict($data);
  311. } else if (is_integer($data) || is_float($data)) {
  312. $data = sprintf('%.0f', round($data, 0));
  313. return $this->_encodeInteger($data);
  314. } else {
  315. return $this->_encodeString($data);
  316. }
  317. }
  318. /**
  319. * Encode an integer into a bencode integer
  320. *
  321. * @param integer $data The integer to be encoded.
  322. * @return string Returns the bencoded integer.
  323. */
  324. private function _encodeInteger($data = null)
  325. {
  326. $data = is_null($data) ? $this->_data : $data;
  327. return sprintf('i%.0fe', $data);
  328. }
  329. /**
  330. * Encode a string into a bencode string
  331. *
  332. * @param string $data The string to be encoded.
  333. * @return string Returns the bencoded string.
  334. */
  335. private function _encodeString($data = null)
  336. {
  337. $data = is_null($data) ? $this->_data : $data;
  338. return sprintf('%d:%s', strlen($data), $data);
  339. }
  340. /**
  341. * Encode a numeric array into a bencode list
  342. *
  343. * @param array $data The numerically indexed array to be encoded.
  344. * @return string Returns the bencoded list.
  345. */
  346. private function _encodeList(array $data = null)
  347. {
  348. $data = is_null($data) ? $this->_data : $data;
  349. $list = '';
  350. foreach ($data as $value) {
  351. $list .= $this->_encode($value);
  352. }
  353. return "l{$list}e";
  354. }
  355. /**
  356. * Encode an associative array into a bencode dictionary
  357. *
  358. * @param array $data The associative array to be encoded.
  359. * @return string Returns the bencoded dictionary.
  360. */
  361. private function _encodeDict(array $data = null)
  362. {
  363. $data = is_null($data) ? $this->_data : $data;
  364. ksort($data); // bencode spec requires dicts to be sorted alphabetically
  365. $dict = '';
  366. foreach ($data as $key => $value) {
  367. $dict .= $this->_encodeString($key) . $this->_encode($value);
  368. }
  369. return "d{$dict}e";
  370. }
  371. }