BinaryStream.php 6.9 KB

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