PageRenderTime 59ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/Amf/Util/BinaryStream.php

https://bitbucket.org/gkawka/zend-framework
PHP | 285 lines | 115 code | 27 blank | 143 comment | 5 complexity | e72042d02676e1de5c1580332c0e74e1 MD5 | raw file
  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: BinaryStream.php 24593 2012-01-05 20:35:02Z matthew $
  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. * Constructor
  50. *
  51. * Create a reference 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. $this->_bigEndian = (pack('l', 1) === "\x00\x00\x00\x01");
  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) > $this->_streamLength) {
  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. if (($this->_needle + 1) > $this->_streamLength) {
  117. require_once 'Zend/Amf/Exception.php';
  118. throw new Zend_Amf_Exception('Buffer underrun at needle position: ' . $this->_needle . ' while requesting length: ' . $length);
  119. }
  120. return ord($this->_stream{$this->_needle++});
  121. }
  122. /**
  123. * Writes the passed string into a signed byte on the stream.
  124. *
  125. * @param string $stream
  126. * @return Zend_Amf_Util_BinaryStream
  127. */
  128. public function writeByte($stream)
  129. {
  130. $this->_stream.= pack('c', $stream);
  131. return $this;
  132. }
  133. /**
  134. * Reads a signed 32-bit integer from the data stream.
  135. *
  136. * @return int Value is in the range of -2147483648 to 2147483647
  137. */
  138. public function readInt()
  139. {
  140. return ($this->readByte() << 8) + $this->readByte();
  141. }
  142. /**
  143. * Write an the integer to the output stream as a 32 bit signed integer
  144. *
  145. * @param int $stream
  146. * @return Zend_Amf_Util_BinaryStream
  147. */
  148. public function writeInt($stream)
  149. {
  150. $this->_stream.= pack('n', $stream);
  151. return $this;
  152. }
  153. /**
  154. * Reads a UTF-8 string from the data stream
  155. *
  156. * @return string A UTF-8 string produced by the byte representation of characters
  157. */
  158. public function readUtf()
  159. {
  160. $length = $this->readInt();
  161. return $this->readBytes($length);
  162. }
  163. /**
  164. * Wite a UTF-8 string to the outputstream
  165. *
  166. * @param string $stream
  167. * @return Zend_Amf_Util_BinaryStream
  168. */
  169. public function writeUtf($stream)
  170. {
  171. $this->writeInt(strlen($stream));
  172. $this->_stream.= $stream;
  173. return $this;
  174. }
  175. /**
  176. * Read a long UTF string
  177. *
  178. * @return string
  179. */
  180. public function readLongUtf()
  181. {
  182. $length = $this->readLong();
  183. return $this->readBytes($length);
  184. }
  185. /**
  186. * Write a long UTF string to the buffer
  187. *
  188. * @param string $stream
  189. * @return Zend_Amf_Util_BinaryStream
  190. */
  191. public function writeLongUtf($stream)
  192. {
  193. $this->writeLong(strlen($stream));
  194. $this->_stream.= $stream;
  195. }
  196. /**
  197. * Read a long numeric value
  198. *
  199. * @return double
  200. */
  201. public function readLong()
  202. {
  203. return ($this->readByte() << 24) + ($this->readByte() << 16) + ($this->readByte() << 8) + $this->readByte();
  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. return (($byte1 << 8) | $byte2);
  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. if (!$this->_bigEndian) {
  238. $bytes = strrev($bytes);
  239. }
  240. $double = unpack('dflt', $bytes);
  241. return $double['flt'];
  242. }
  243. /**
  244. * Writes an IEEE 754 double-precision floating point number from the data stream.
  245. *
  246. * @param string|double $stream
  247. * @return Zend_Amf_Util_BinaryStream
  248. */
  249. public function writeDouble($stream)
  250. {
  251. $stream = pack('d', $stream);
  252. if (!$this->_bigEndian) {
  253. $stream = strrev($stream);
  254. }
  255. $this->_stream.= $stream;
  256. return $this;
  257. }
  258. }