BinaryStream.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: BinaryStream.php 2504 2011-12-28 07:35:29Z liu21st $
  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-2009 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. * Constructor
  50. *
  51. * Create a refrence to a byte stream that is going to be parsed or created
  52. * by the methods in the class. Detect if the class should use big or
  53. * little Endian encoding.
  54. *
  55. * @param string $stream use '' if creating a new stream or pass a string if reading.
  56. * @return void
  57. */
  58. public function __construct($stream)
  59. {
  60. if (!is_string($stream)) {
  61. require_once 'Zend/Amf/Exception.php';
  62. throw new Zend_Amf_Exception('Inputdata is not of type String');
  63. }
  64. $this->_stream = $stream;
  65. $this->_needle = 0;
  66. $this->_streamLength = strlen($stream);
  67. $testEndian = unpack("C*", pack("S*", 256));
  68. $this->_bigEndian = 1;
  69. }
  70. /**
  71. * Returns the current stream
  72. *
  73. * @return string
  74. */
  75. public function getStream()
  76. {
  77. return $this->_stream;
  78. }
  79. /**
  80. * Read the number of bytes in a row for the length supplied.
  81. *
  82. * @todo Should check that there are enough bytes left in the stream we are about to read.
  83. * @param int $length
  84. * @return string
  85. * @throws Zend_Amf_Exception for buffer underrun
  86. */
  87. public function readBytes($length)
  88. {
  89. if (($length + $this->_needle) > strlen($this->_stream)) {
  90. require_once 'Zend/Amf/Exception.php';
  91. throw new Zend_Amf_Exception("Buffer underrun at needle position: " . $this->_needle . " while requesting length: " . $length);
  92. }
  93. $bytes = substr($this->_stream, $this->_needle, $length);
  94. $this->_needle += $length;
  95. return $bytes;
  96. }
  97. /**
  98. * Write any length of bytes to the stream
  99. *
  100. * Usually a string.
  101. *
  102. * @param string $bytes
  103. * @return Zend_Amf_Util_BinaryStream
  104. */
  105. public function writeBytes($bytes)
  106. {
  107. $this->_stream .= $bytes;
  108. return $this;
  109. }
  110. /**
  111. * Reads a signed byte
  112. *
  113. * @return int Value is in the range of -128 to 127.
  114. */
  115. public function readByte()
  116. {
  117. $byte = ord($this->_stream[$this->_needle++]);
  118. return $byte;
  119. }
  120. /**
  121. * Writes the passed string into a signed byte on the stream.
  122. *
  123. * @param string $stream
  124. * @return Zend_Amf_Util_BinaryStream
  125. */
  126. public function writeByte($stream)
  127. {
  128. $this->_stream .= pack("c",$stream);
  129. return $this;
  130. }
  131. /**
  132. * Reads a signed 32-bit integer from the data stream.
  133. *
  134. * @return int Value is in the range of -2147483648 to 2147483647
  135. */
  136. public function readInt()
  137. {
  138. $int = ($this->readByte() << 8) + $this->readByte();
  139. return $int;
  140. }
  141. /**
  142. * Write an the integer to the output stream as a 32 bit signed integer
  143. *
  144. * @param int $stream
  145. * @return Zend_Amf_Util_BinaryStream
  146. */
  147. public function writeInt($stream)
  148. {
  149. $this->_stream .= pack("n", $stream);
  150. return $this;
  151. }
  152. /**
  153. * Reads a UTF-8 string from the data stream
  154. *
  155. * @return string A UTF-8 string produced by the byte representation of characters
  156. */
  157. public function readUtf()
  158. {
  159. $length = $this->readInt();
  160. return $this->readBytes($length);
  161. }
  162. /**
  163. * Wite a UTF-8 string to the outputstream
  164. *
  165. * @param string $stream
  166. * @return Zend_Amf_Util_BinaryStream
  167. */
  168. public function writeUtf($stream)
  169. {
  170. $this->writeInt(strlen($stream));
  171. $this->_stream .= $stream;
  172. return $this;
  173. }
  174. /**
  175. * Read a long UTF string
  176. *
  177. * @return string
  178. */
  179. public function readLongUtf()
  180. {
  181. $length = $this->readLong();
  182. return $this->readBytes($length);
  183. }
  184. /**
  185. * Write a long UTF string to the buffer
  186. *
  187. * @param string $stream
  188. * @return Zend_Amf_Util_BinaryStream
  189. */
  190. public function writeLongUtf($stream)
  191. {
  192. $this->writeLong(strlen($stream));
  193. $this->_stream .= $stream;
  194. }
  195. /**
  196. * Read a long numeric value
  197. *
  198. * @return double
  199. */
  200. public function readLong()
  201. {
  202. $long = ($this->readByte() << 24) + ($this->readByte() << 16) + ($this->readByte() << 8) + $this->readByte();
  203. return $long;
  204. }
  205. /**
  206. * Write long numeric value to output stream
  207. *
  208. * @param int|string $stream
  209. * @return Zend_Amf_Util_BinaryStream
  210. */
  211. public function writeLong($stream)
  212. {
  213. $this->_stream .= pack("N",$stream);
  214. return $this;
  215. }
  216. /**
  217. * Read a 16 bit unsigned short.
  218. *
  219. * @todo This could use the unpack() w/ S,n, or v
  220. * @return double
  221. */
  222. public function readUnsignedShort()
  223. {
  224. $byte1 = $this->readByte();
  225. $byte2 = $this->readByte();
  226. $short = (($byte1 << 8) | $byte2);
  227. return $short;
  228. }
  229. /**
  230. * Reads an IEEE 754 double-precision floating point number from the data stream.
  231. *
  232. * @return double Floating point number
  233. */
  234. public function readDouble()
  235. {
  236. $bytes = substr($this->_stream, $this->_needle, 8);
  237. $this->_needle += 8;
  238. $double = unpack("dflt", strrev($bytes));
  239. return $double['flt'];
  240. }
  241. /**
  242. * Writes an IEEE 754 double-precision floating point number from the data stream.
  243. *
  244. * @param string|double $stream
  245. * @return Zend_Amf_Util_BinaryStream
  246. */
  247. public function writeDouble($stream)
  248. {
  249. $stream = pack("d", $stream);
  250. if ($this->_bigEndian) {
  251. $stream = strrev($stream);
  252. }
  253. $this->_stream .= $stream;
  254. return $this;
  255. }
  256. }