BioTorrents.de’s version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Bencode.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace OrpheusNET\BencodeTorrent;
  3. class Bencode {
  4. protected $data = null;
  5. /**
  6. * Sets the internal data array
  7. * @param mixed $data
  8. * @throws \RuntimeException
  9. */
  10. public function setData($data) {
  11. $this->data = $data;
  12. }
  13. /**
  14. * Given a BEncoded string and decode it
  15. * @param string $data
  16. * @throws \RuntimeException
  17. */
  18. public function decodeString(string $data) {
  19. $this->data = $this->decode($data);
  20. }
  21. /**
  22. * Given a path to a file, decode the contents of it
  23. *
  24. * @param string $path
  25. * @throws \RuntimeException
  26. */
  27. public function decodeFile(string $path) {
  28. $this->data = $this->decode(file_get_contents($path, FILE_BINARY));
  29. }
  30. /**
  31. * Decodes a BEncoded string to the following values:
  32. * - Dictionary (starts with d, ends with e)
  33. * - List (starts with l, ends with e
  34. * - Integer (starts with i, ends with e
  35. * - String (starts with number denoting number of characters followed by : and then the string)
  36. *
  37. * @see https://wiki.theory.org/index.php/BitTorrentSpecification
  38. *
  39. * @param string $data
  40. * @param int $pos
  41. * @return mixed
  42. */
  43. protected function decode(string $data, int &$pos = 0) {
  44. $start_decode = $pos === 0;
  45. if ($data[$pos] === 'd') {
  46. $pos++;
  47. $return = [];
  48. while ($data[$pos] !== 'e') {
  49. $key = $this->decode($data, $pos);
  50. $value = $this->decode($data, $pos);
  51. if ($key === null || $value === null) {
  52. break;
  53. }
  54. if (!is_string($key)) {
  55. throw new \RuntimeException('Invalid key type, must be string: '.gettype($key));
  56. }
  57. $return[$key] = $value;
  58. }
  59. ksort($return);
  60. $pos++;
  61. }
  62. elseif ($data[$pos] === 'l') {
  63. $pos++;
  64. $return = [];
  65. while ($data[$pos] !== 'e') {
  66. $value = $this->decode($data, $pos);
  67. $return[] = $value;
  68. }
  69. $pos++;
  70. }
  71. elseif ($data[$pos] === 'i') {
  72. $pos++;
  73. $digits = strpos($data, 'e', $pos) - $pos;
  74. $return = substr($data, $pos, $digits);
  75. if ($return === '-0') {
  76. throw new \RuntimeException('Cannot have integer value -0');
  77. }
  78. $multiplier = 1;
  79. if ($return[0] === '-') {
  80. $multiplier = -1;
  81. $return = substr($return, 1);
  82. }
  83. if (!ctype_digit($return)) {
  84. $msg = 'Cannot have non-digit values in integer number: '.$return;
  85. throw new \RuntimeException($msg);
  86. }
  87. $return = $multiplier * ((int) $return);
  88. $pos += $digits + 1;
  89. }
  90. else {
  91. $digits = strpos($data, ':', $pos) - $pos;
  92. $len = (int) substr($data, $pos, $digits);
  93. $pos += ($digits + 1);
  94. $return = substr($data, $pos, $len);
  95. $pos += $len;
  96. }
  97. if ($start_decode) {
  98. if ($pos !== strlen($data)) {
  99. throw new \RuntimeException('Could not fully decode bencode string');
  100. }
  101. }
  102. return $return;
  103. }
  104. /**
  105. * Get the internal data array
  106. * @return mixed
  107. */
  108. public function getData() {
  109. return $this->data;
  110. }
  111. /**
  112. * @throws \RuntimeException
  113. */
  114. protected function hasData() {
  115. if ($this->data === null) {
  116. throw new \RuntimeException('Must decode proper bencode string first');
  117. }
  118. }
  119. /**
  120. * @return string
  121. */
  122. public function getEncode() : string {
  123. $this->hasData();
  124. return $this->encodeVal($this->data);
  125. }
  126. /**
  127. * @param mixed $data
  128. * @return string
  129. */
  130. protected function encodeVal($data) : string {
  131. if (is_array($data)) {
  132. $return = '';
  133. $check = -1;
  134. $list = true;
  135. foreach ($data as $key => $value) {
  136. if ($key !== ++$check) {
  137. $list = false;
  138. break;
  139. }
  140. }
  141. if ($list) {
  142. $return .= 'l';
  143. foreach ($data as $value) {
  144. $return .= $this->encodeVal($value);
  145. }
  146. }
  147. else {
  148. $return .= 'd';
  149. foreach ($data as $key => $value) {
  150. $return .= $this->encodeVal(strval($key));
  151. $return .= $this->encodeVal($value);
  152. }
  153. }
  154. $return .= 'e';
  155. }
  156. elseif (is_integer($data)) {
  157. $return = 'i'.$data.'e';
  158. }
  159. else {
  160. $return = strlen($data) . ':' . $data;
  161. }
  162. return $return;
  163. }
  164. }