BDecode.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /* $Id: BDecode.php 467 2006-08-26 07:04:59Z b4rt $ */
  3. /*
  4. Programming info
  5. All functions output a small array, which we'll call $return for now.
  6. $return[0] is the data expected of the function
  7. $return[1] is the offset over the whole bencoded data of the next
  8. piece of data.
  9. numberdecode returns [0] as the integer read, and [1]-1 points to the
  10. symbol that was interprented as the end of the interger (either "e" or
  11. ":").
  12. numberdecode is used for integer decodes both for i11e and 11:hello there
  13. so it is tolerant of the ending symbol.
  14. decodelist returns $return[0] as an integer indexed array like you would use in C
  15. for all the entries. $return[1]-1 is the "e" that ends the list, so [1] is the next
  16. useful byte.
  17. decodeDict returns $return[0] as an array of text-indexed entries. For example,
  18. $return[0]["announce"] = "http://www.whatever.com:6969/announce";
  19. $return[1]-1 again points to the "e" that ends the dictionary.
  20. decodeEntry returns [0] as an integer in the case $offset points to
  21. i12345e or a string if $offset points to 11:hello there style strings.
  22. It also calls decodeDict or decodeList if it encounters a d or an l.
  23. Known bugs:
  24. - The program doesn't pay attention to the string it's working on.
  25. A zero-sized or truncated data block will cause string offset errors
  26. before they get rejected by the decoder. This is worked around by
  27. suppressing errors.
  28. */
  29. // Protect our namespace using a class
  30. class BDecode
  31. {
  32. function numberdecode($wholefile, $start)
  33. {
  34. $ret[0] = 0;
  35. $offset = $start;
  36. // Funky handling of negative numbers and zero
  37. $negative = false;
  38. if ($wholefile[$offset] == '-')
  39. {
  40. $negative = true;
  41. $offset++;
  42. }
  43. if ($wholefile[$offset] == '0')
  44. {
  45. $offset++;
  46. if ($negative)
  47. {
  48. return array(false);
  49. }
  50. if ($wholefile[$offset] == ':' || $wholefile[$offset] == 'e')
  51. {
  52. $offset++;
  53. $ret[0] = 0;
  54. $ret[1] = $offset;
  55. return $ret;
  56. }
  57. return array(false);
  58. }
  59. while (true)
  60. {
  61. if ($wholefile[$offset] >= '0' && $wholefile[$offset] <= '9')
  62. {
  63. $ret[0] *= 10;
  64. //Added 2005.02.21 - VisiGod
  65. //Changing the type of variable from integer to double to prevent a numeric overflow
  66. settype($ret[0],"double");
  67. //Added 2005.02.21 - VisiGod
  68. $ret[0] += ord($wholefile[$offset]) - ord("0");
  69. $offset++;
  70. }
  71. // Tolerate : or e because this is a multiuse function
  72. else if ($wholefile[$offset] == 'e' || $wholefile[$offset] == ':')
  73. {
  74. $ret[1] = $offset+1;
  75. if ($negative)
  76. {
  77. if ($ret[0] == 0)
  78. {
  79. return array(false);
  80. }
  81. $ret[0] = - $ret[0];
  82. }
  83. return $ret;
  84. }
  85. else
  86. {
  87. return array(false);
  88. }
  89. }
  90. return array(false);
  91. }
  92. function decodeEntry($wholefile, $offset=0)
  93. {
  94. if ($wholefile[$offset] == 'd')
  95. {
  96. return $this->decodeDict($wholefile, $offset);
  97. }
  98. if ($wholefile[$offset] == 'l')
  99. {
  100. return $this->decodelist($wholefile, $offset);
  101. }
  102. if ($wholefile[$offset] == "i")
  103. {
  104. $offset++;
  105. return $this->numberdecode($wholefile, $offset);
  106. }
  107. // String value: decode number, then grab substring
  108. $info = $this->numberdecode($wholefile, $offset);
  109. if ($info[0] === false)
  110. {
  111. return array(false);
  112. }
  113. $ret[0] = substr($wholefile, $info[1], $info[0]);
  114. $ret[1] = $info[1]+strlen($ret[0]);
  115. return $ret;
  116. }
  117. function decodeList($wholefile, $start)
  118. {
  119. $offset = $start+1;
  120. $i = 0;
  121. if ($wholefile[$start] != 'l')
  122. {
  123. return array(false);
  124. }
  125. $ret = array();
  126. while (true)
  127. {
  128. if ($wholefile[$offset] == 'e')
  129. {
  130. break;
  131. }
  132. $value = $this->decodeEntry($wholefile, $offset);
  133. if ($value[0] === false)
  134. {
  135. return array(false);
  136. }
  137. $ret[$i] = $value[0];
  138. $offset = $value[1];
  139. $i ++;
  140. }
  141. // The empy list is an empty array. Seems fine.
  142. $final[0] = $ret;
  143. $final[1] = $offset+1;
  144. return $final;
  145. }
  146. // Tries to construct an array
  147. function decodeDict($wholefile, $start=0)
  148. {
  149. $offset = $start;
  150. if ($wholefile[$offset] == 'l')
  151. {
  152. return $this->decodeList($wholefile, $start);
  153. }
  154. if ($wholefile[$offset] != 'd')
  155. {
  156. return false;
  157. }
  158. $ret = array();
  159. $offset++;
  160. while (true)
  161. {
  162. if ($wholefile[$offset] == 'e')
  163. {
  164. $offset++;
  165. break;
  166. }
  167. $left = $this->decodeEntry($wholefile, $offset);
  168. if (!$left[0])
  169. {
  170. return false;
  171. }
  172. $offset = $left[1];
  173. if ($wholefile[$offset] == 'd')
  174. {
  175. // Recurse
  176. $value = $this->decodedict($wholefile, $offset);
  177. if (!$value[0])
  178. {
  179. return false;
  180. }
  181. $ret[addslashes($left[0])] = $value[0];
  182. $offset= $value[1];
  183. continue;
  184. }
  185. else if ($wholefile[$offset] == 'l')
  186. {
  187. $value = $this->decodeList($wholefile, $offset);
  188. if (!$value[0] && is_bool($value[0]))
  189. {
  190. return false;
  191. }
  192. $ret[addslashes($left[0])] = $value[0];
  193. $offset = $value[1];
  194. }
  195. else
  196. {
  197. $value = $this->decodeEntry($wholefile, $offset);
  198. if ($value[0] === false)
  199. {
  200. return false;
  201. }
  202. $ret[addslashes($left[0])] = $value[0];
  203. $offset = $value[1];
  204. }
  205. }
  206. if (empty($ret))
  207. {
  208. $final[0] = true;
  209. }
  210. else
  211. {
  212. $final[0] = $ret;
  213. }
  214. $final[1] = $offset;
  215. return $final;
  216. }
  217. } // End of class declaration.
  218. // Use this function. eg: BDecode("d8:announce44:http://www. ... e");
  219. function BDecode($wholefile)
  220. {
  221. $decoder = new BDecode;
  222. $return = $decoder->decodeEntry($wholefile);
  223. return $return[0];
  224. }
  225. ?>