BinaryStream.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Amf
  17. * @subpackage Util
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Utility class to walk through a data stream byte by byte with conventional names
  24. *
  25. * @package Zend_Amf
  26. * @subpackage Util
  27. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. */
  30. class Zend_Amf_Util_BinaryStream
  31. {
  32. /**
  33. * @var string Byte stream
  34. */
  35. protected $_stream;
  36. /**
  37. * @var int Length of stream
  38. */
  39. protected $_streamLength;
  40. /**
  41. * @var bool BigEndian encoding?
  42. */
  43. protected $_bigEndian;
  44. /**
  45. * @var int Current position in stream
  46. */
  47. protected $_needle;
  48. /**
  49. * @var bool str* functions overloaded using mbstring.func_overload?
  50. */
  51. protected $_mbStringFunctionsOverloaded;
  52. /**
  53. * Constructor
  54. *
  55. * Create a reference to a byte stream that is going to be parsed or created
  56. * by the methods in the class. Detect if the class should use big or
  57. * little Endian encoding.
  58. *
  59. * @param string $stream use '' if creating a new stream or pass a string if reading.
  60. * @return void
  61. */
  62. public function __construct($stream)
  63. {
  64. if (!is_string($stream)) {
  65. require_once 'Zend/Amf/Exception.php';
  66. throw new Zend_Amf_Exception('Inputdata is not of type String');
  67. }
  68. $this->_stream = $stream;
  69. $this->_needle = 0;
  70. $this->_mbStringFunctionsOverloaded = function_exists('mb_strlen') && (ini_get('mbstring.func_overload') !== '') && ((int)ini_get('mbstring.func_overload') & 2);
  71. $this->_streamLength = $this->_mbStringFunctionsOverloaded ? mb_strlen($stream, '8bit') : strlen($stream);
  72. $this->_bigEndian = (pack('l', 1) === "\x00\x00\x00\x01");
  73. }
  74. /**
  75. * Returns the current stream
  76. *
  77. * @return string
  78. */
  79. public function getStream()
  80. {
  81. return $this->_stream;
  82. }
  83. /**
  84. * Read the number of bytes in a row for the length supplied.
  85. *
  86. * @todo Should check that there are enough bytes left in the stream we are about to read.
  87. * @param int $length
  88. * @return string
  89. * @throws Zend_Amf_Exception for buffer underrun
  90. */
  91. public function readBytes($length)
  92. {
  93. if (($length + $this->_needle) > $this->_streamLength) {
  94. require_once 'Zend/Amf/Exception.php';
  95. throw new Zend_Amf_Exception('Buffer underrun at needle position: ' . $this->_needle . ' while requesting length: ' . $length);
  96. }
  97. $bytes = $this->_mbStringFunctionsOverloaded ? mb_substr($this->_stream, $this->_needle, $length, '8bit') : substr($this->_stream, $this->_needle, $length);
  98. $this->_needle+= $length;
  99. return $bytes;
  100. }
  101. /**
  102. * Write any length of bytes to the stream
  103. *
  104. * Usually a string.
  105. *
  106. * @param string $bytes
  107. * @return Zend_Amf_Util_BinaryStream
  108. */
  109. public function writeBytes($bytes)
  110. {
  111. $this->_stream.= $bytes;
  112. return $this;
  113. }
  114. /**
  115. * Reads a signed byte
  116. *
  117. * @return int Value is in the range of -128 to 127.
  118. * @throws Zend_Amf_Exception
  119. */
  120. public function readByte()
  121. {
  122. if (($this->_needle + 1) > $this->_streamLength) {
  123. require_once 'Zend/Amf/Exception.php';
  124. throw new Zend_Amf_Exception(
  125. 'Buffer underrun at needle position: '
  126. . $this->_needle
  127. . ' while requesting length: '
  128. . $this->_streamLength
  129. );
  130. }
  131. return ord($this->_stream{$this->_needle++});
  132. }
  133. /**
  134. * Writes the passed string into a signed byte on the stream.
  135. *
  136. * @param string $stream
  137. * @return Zend_Amf_Util_BinaryStream
  138. */
  139. public function writeByte($stream)
  140. {
  141. $this->_stream.= pack('c', $stream);
  142. return $this;
  143. }
  144. /**
  145. * Reads a signed 32-bit integer from the data stream.
  146. *
  147. * @return int Value is in the range of -2147483648 to 2147483647
  148. */
  149. public function readInt()
  150. {
  151. return ($this->readByte() << 8) + $this->readByte();
  152. }
  153. /**
  154. * Write an the integer to the output stream as a 32 bit signed integer
  155. *
  156. * @param int $stream
  157. * @return Zend_Amf_Util_BinaryStream
  158. */
  159. public function writeInt($stream)
  160. {
  161. $this->_stream.= pack('n', $stream);
  162. return $this;
  163. }
  164. /**
  165. * Reads a UTF-8 string from the data stream
  166. *
  167. * @return string A UTF-8 string produced by the byte representation of characters
  168. */
  169. public function readUtf()
  170. {
  171. $length = $this->readInt();
  172. return $this->readBytes($length);
  173. }
  174. /**
  175. * Wite a UTF-8 string to the outputstream
  176. *
  177. * @param string $stream
  178. * @return Zend_Amf_Util_BinaryStream
  179. */
  180. public function writeUtf($stream)
  181. {
  182. $this->writeInt($this->_mbStringFunctionsOverloaded ? mb_strlen($stream, '8bit') : strlen($stream));
  183. $this->_stream.= $stream;
  184. return $this;
  185. }
  186. /**
  187. * Read a long UTF string
  188. *
  189. * @return string
  190. */
  191. public function readLongUtf()
  192. {
  193. $length = $this->readLong();
  194. return $this->readBytes($length);
  195. }
  196. /**
  197. * Write a long UTF string to the buffer
  198. *
  199. * @param string $stream
  200. * @return Zend_Amf_Util_BinaryStream
  201. */
  202. public function writeLongUtf($stream)
  203. {
  204. $this->writeLong($this->_mbStringFunctionsOverloaded ? mb_strlen($stream, '8bit') : strlen($stream));
  205. $this->_stream.= $stream;
  206. }
  207. /**
  208. * Read a long numeric value
  209. *
  210. * @return double
  211. */
  212. public function readLong()
  213. {
  214. return ($this->readByte() << 24) + ($this->readByte() << 16) + ($this->readByte() << 8) + $this->readByte();
  215. }
  216. /**
  217. * Write long numeric value to output stream
  218. *
  219. * @param int|string $stream
  220. * @return Zend_Amf_Util_BinaryStream
  221. */
  222. public function writeLong($stream)
  223. {
  224. $this->_stream.= pack('N', $stream);
  225. return $this;
  226. }
  227. /**
  228. * Read a 16 bit unsigned short.
  229. *
  230. * @todo This could use the unpack() w/ S,n, or v
  231. * @return double
  232. */
  233. public function readUnsignedShort()
  234. {
  235. $byte1 = $this->readByte();
  236. $byte2 = $this->readByte();
  237. return (($byte1 << 8) | $byte2);
  238. }
  239. /**
  240. * Reads an IEEE 754 double-precision floating point number from the data stream.
  241. *
  242. * @return double Floating point number
  243. */
  244. public function readDouble()
  245. {
  246. $bytes = $this->_mbStringFunctionsOverloaded ? mb_substr($this->_stream, $this->_needle, 8, '8bit') : substr($this->_stream, $this->_needle, 8);
  247. $this->_needle+= 8;
  248. if (!$this->_bigEndian) {
  249. $bytes = strrev($bytes);
  250. }
  251. $double = unpack('dflt', $bytes);
  252. return $double['flt'];
  253. }
  254. /**
  255. * Writes an IEEE 754 double-precision floating point number from the data stream.
  256. *
  257. * @param string|double $stream
  258. * @return Zend_Amf_Util_BinaryStream
  259. */
  260. public function writeDouble($stream)
  261. {
  262. $stream = pack('d', $stream);
  263. if (!$this->_bigEndian) {
  264. $stream = strrev($stream);
  265. }
  266. $this->_stream.= $stream;
  267. return $this;
  268. }
  269. }