web/lib/Zend/Amf/Util/BinaryStream.php
changeset 807 877f952ae2bd
parent 207 621fa6caec0c
child 1230 68c69c656a2c
equal deleted inserted replaced
805:5e7a0fedabdf 807:877f952ae2bd
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Amf
    16  * @package    Zend_Amf
    17  * @subpackage Util
    17  * @subpackage Util
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    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
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    20  * @version    $Id: BinaryStream.php 22101 2010-05-04 20:07:13Z matthew $
    20  * @version    $Id: BinaryStream.php 25241 2013-01-22 11:07:36Z frosch $
    21  */
    21  */
    22 
    22 
    23 /**
    23 /**
    24  * Utility class to walk through a data stream byte by byte with conventional names
    24  * Utility class to walk through a data stream byte by byte with conventional names
    25  *
    25  *
    26  * @package    Zend_Amf
    26  * @package    Zend_Amf
    27  * @subpackage Util
    27  * @subpackage Util
    28  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    28  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    29  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    29  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    30  */
    30  */
    31 class Zend_Amf_Util_BinaryStream
    31 class Zend_Amf_Util_BinaryStream
    32 {
    32 {
    33     /**
    33     /**
    49      * @var int Current position in stream
    49      * @var int Current position in stream
    50      */
    50      */
    51     protected $_needle;
    51     protected $_needle;
    52 
    52 
    53     /**
    53     /**
       
    54      * @var bool str* functions overloaded using mbstring.func_overload?
       
    55      */
       
    56     protected $_mbStringFunctionsOverloaded;
       
    57 
       
    58     /**
    54      * Constructor
    59      * Constructor
    55      *
    60      *
    56      * Create a reference to a byte stream that is going to be parsed or created
    61      * Create a reference to a byte stream that is going to be parsed or created
    57      * by the methods in the class. Detect if the class should use big or
    62      * by the methods in the class. Detect if the class should use big or
    58      * little Endian encoding.
    63      * little Endian encoding.
    67             throw new Zend_Amf_Exception('Inputdata is not of type String');
    72             throw new Zend_Amf_Exception('Inputdata is not of type String');
    68         }
    73         }
    69 
    74 
    70         $this->_stream       = $stream;
    75         $this->_stream       = $stream;
    71         $this->_needle       = 0;
    76         $this->_needle       = 0;
    72         $this->_streamLength = strlen($stream);
    77         $this->_mbStringFunctionsOverloaded = function_exists('mb_strlen') && (ini_get('mbstring.func_overload') !== '') && ((int)ini_get('mbstring.func_overload') & 2);
       
    78         $this->_streamLength = $this->_mbStringFunctionsOverloaded ? mb_strlen($stream, '8bit') : strlen($stream);
    73         $this->_bigEndian    = (pack('l', 1) === "\x00\x00\x00\x01");
    79         $this->_bigEndian    = (pack('l', 1) === "\x00\x00\x00\x01");
    74     }
    80     }
    75 
    81 
    76     /**
    82     /**
    77      * Returns the current stream
    83      * Returns the current stream
    95     {
   101     {
    96         if (($length + $this->_needle) > $this->_streamLength) {
   102         if (($length + $this->_needle) > $this->_streamLength) {
    97             require_once 'Zend/Amf/Exception.php';
   103             require_once 'Zend/Amf/Exception.php';
    98             throw new Zend_Amf_Exception('Buffer underrun at needle position: ' . $this->_needle . ' while requesting length: ' . $length);
   104             throw new Zend_Amf_Exception('Buffer underrun at needle position: ' . $this->_needle . ' while requesting length: ' . $length);
    99         }
   105         }
   100         $bytes = substr($this->_stream, $this->_needle, $length);
   106         $bytes = $this->_mbStringFunctionsOverloaded ? mb_substr($this->_stream, $this->_needle, $length, '8bit') : substr($this->_stream, $this->_needle, $length);
   101         $this->_needle+= $length;
   107         $this->_needle+= $length;
   102         return $bytes;
   108         return $bytes;
   103     }
   109     }
   104 
   110 
   105     /**
   111     /**
   118 
   124 
   119     /**
   125     /**
   120      * Reads a signed byte
   126      * Reads a signed byte
   121      *
   127      *
   122      * @return int Value is in the range of -128 to 127.
   128      * @return int Value is in the range of -128 to 127.
       
   129      * @throws Zend_Amf_Exception
   123      */
   130      */
   124     public function readByte()
   131     public function readByte()
   125     {
   132     {
   126         if (($this->_needle + 1) > $this->_streamLength) {
   133         if (($this->_needle + 1) > $this->_streamLength) {
   127             require_once 'Zend/Amf/Exception.php';
   134             require_once 'Zend/Amf/Exception.php';
   128             throw new Zend_Amf_Exception('Buffer underrun at needle position: ' . $this->_needle . ' while requesting length: ' . $length);
   135             throw new Zend_Amf_Exception(
       
   136                 'Buffer underrun at needle position: '
       
   137                 . $this->_needle
       
   138                 . ' while requesting length: '
       
   139                 . $this->_streamLength
       
   140             );
   129         }
   141         }
   130 
   142 
   131         return ord($this->_stream{$this->_needle++});
   143         return ord($this->_stream{$this->_needle++});
   132     }
   144     }
   133 
   145 
   182      * @param  string $stream
   194      * @param  string $stream
   183      * @return Zend_Amf_Util_BinaryStream
   195      * @return Zend_Amf_Util_BinaryStream
   184      */
   196      */
   185     public function writeUtf($stream)
   197     public function writeUtf($stream)
   186     {
   198     {
   187         $this->writeInt(strlen($stream));
   199         $this->writeInt($this->_mbStringFunctionsOverloaded ? mb_strlen($stream, '8bit') : strlen($stream));
   188         $this->_stream.= $stream;
   200         $this->_stream.= $stream;
   189         return $this;
   201         return $this;
   190     }
   202     }
   191 
   203 
   192 
   204 
   207      * @param  string $stream
   219      * @param  string $stream
   208      * @return Zend_Amf_Util_BinaryStream
   220      * @return Zend_Amf_Util_BinaryStream
   209      */
   221      */
   210     public function writeLongUtf($stream)
   222     public function writeLongUtf($stream)
   211     {
   223     {
   212         $this->writeLong(strlen($stream));
   224         $this->writeLong($this->_mbStringFunctionsOverloaded ? mb_strlen($stream, '8bit') : strlen($stream));
   213         $this->_stream.= $stream;
   225         $this->_stream.= $stream;
   214     }
   226     }
   215 
   227 
   216     /**
   228     /**
   217      * Read a long numeric value
   229      * Read a long numeric value
   253      *
   265      *
   254      * @return double Floating point number
   266      * @return double Floating point number
   255      */
   267      */
   256     public function readDouble()
   268     public function readDouble()
   257     {
   269     {
   258         $bytes = substr($this->_stream, $this->_needle, 8);
   270         $bytes = $this->_mbStringFunctionsOverloaded ? mb_substr($this->_stream, $this->_needle, 8, '8bit') : substr($this->_stream, $this->_needle, 8);
   259         $this->_needle+= 8;
   271         $this->_needle+= 8;
   260 
   272 
   261         if (!$this->_bigEndian) {
   273         if (!$this->_bigEndian) {
   262             $bytes = strrev($bytes);
   274             $bytes = strrev($bytes);
   263         }
   275         }