Browse Source

ZF-7342: Endian Detection Issue
+ using of streamLength variable

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@20124 44c647ce-9c0f-0410-b52a-842ac1e357ba

mabe 16 years ago
parent
commit
928a64bacb
1 changed files with 31 additions and 25 deletions
  1. 31 25
      library/Zend/Amf/Util/BinaryStream.php

+ 31 - 25
library/Zend/Amf/Util/BinaryStream.php

@@ -70,8 +70,7 @@ class Zend_Amf_Util_BinaryStream
         $this->_stream       = $stream;
         $this->_needle       = 0;
         $this->_streamLength = strlen($stream);
-        $testEndian          = unpack("C*", pack("S*", 256));
-        $this->_bigEndian    = 1;
+        $this->_bigEndian    = (pack('l', 1) === "\x00\x00\x00\x01");
     }
 
     /**
@@ -94,12 +93,12 @@ class Zend_Amf_Util_BinaryStream
      */
     public function readBytes($length)
     {
-        if (($length + $this->_needle) > strlen($this->_stream)) {
+        if (($length + $this->_needle) > $this->_streamLength) {
             require_once 'Zend/Amf/Exception.php';
-            throw new Zend_Amf_Exception("Buffer underrun at needle position: " . $this->_needle . " while requesting length: " . $length);
+            throw new Zend_Amf_Exception('Buffer underrun at needle position: ' . $this->_needle . ' while requesting length: ' . $length);
         }
         $bytes = substr($this->_stream, $this->_needle, $length);
-        $this->_needle += $length;
+        $this->_needle+= $length;
         return $bytes;
     }
 
@@ -113,7 +112,7 @@ class Zend_Amf_Util_BinaryStream
      */
     public function writeBytes($bytes)
     {
-        $this->_stream .= $bytes;
+        $this->_stream.= $bytes;
         return $this;
     }
 
@@ -124,8 +123,12 @@ class Zend_Amf_Util_BinaryStream
      */
     public function readByte()
     {
-        $byte = ord($this->_stream[$this->_needle++]);
-        return $byte;
+        if (($this->_needle + 1) > $this->_streamLength) {
+            require_once 'Zend/Amf/Exception.php';
+            throw new Zend_Amf_Exception('Buffer underrun at needle position: ' . $this->_needle . ' while requesting length: ' . $length);
+        }
+
+        return ord($this->_stream{$this->_needle++});
     }
 
     /**
@@ -136,7 +139,7 @@ class Zend_Amf_Util_BinaryStream
      */
     public function writeByte($stream)
     {
-        $this->_stream .= pack("c",$stream);
+        $this->_stream.= pack('c', $stream);
         return $this;
     }
 
@@ -147,8 +150,7 @@ class Zend_Amf_Util_BinaryStream
      */
     public function readInt()
     {
-        $int = ($this->readByte() << 8) + $this->readByte();
-        return $int;
+        return ($this->readByte() << 8) + $this->readByte();
     }
 
     /**
@@ -159,7 +161,7 @@ class Zend_Amf_Util_BinaryStream
      */
     public function writeInt($stream)
     {
-        $this->_stream .= pack("n", $stream);
+        $this->_stream.= pack('n', $stream);
         return $this;
     }
 
@@ -183,7 +185,7 @@ class Zend_Amf_Util_BinaryStream
     public function writeUtf($stream)
     {
         $this->writeInt(strlen($stream));
-        $this->_stream .= $stream;
+        $this->_stream.= $stream;
         return $this;
     }
 
@@ -208,7 +210,7 @@ class Zend_Amf_Util_BinaryStream
     public function writeLongUtf($stream)
     {
         $this->writeLong(strlen($stream));
-        $this->_stream .= $stream;
+        $this->_stream.= $stream;
     }
 
     /**
@@ -218,8 +220,7 @@ class Zend_Amf_Util_BinaryStream
      */
     public function readLong()
     {
-        $long = ($this->readByte() << 24) + ($this->readByte() << 16) + ($this->readByte() << 8) + $this->readByte();
-        return $long;
+        return ($this->readByte() << 24) + ($this->readByte() << 16) + ($this->readByte() << 8) + $this->readByte();
     }
 
     /**
@@ -230,7 +231,7 @@ class Zend_Amf_Util_BinaryStream
      */
     public function writeLong($stream)
     {
-        $this->_stream .= pack("N",$stream);
+        $this->_stream.= pack('N', $stream);
         return $this;
     }
 
@@ -244,8 +245,7 @@ class Zend_Amf_Util_BinaryStream
     {
         $byte1 = $this->readByte();
         $byte2 = $this->readByte();
-        $short = (($byte1 << 8) | $byte2);
-        return $short;
+        return (($byte1 << 8) | $byte2);
     }
 
     /**
@@ -255,9 +255,14 @@ class Zend_Amf_Util_BinaryStream
      */
     public function readDouble()
     {
-        $bytes          = substr($this->_stream, $this->_needle, 8);
-        $this->_needle += 8;
-        $double         = unpack("dflt", strrev($bytes));
+        $bytes = substr($this->_stream, $this->_needle, 8);
+        $this->_needle+= 8;
+
+        if (!$this->_bigEndian) {
+            $bytes = strrev($bytes);
+        }
+
+        $double = unpack('dflt', $bytes);
         return $double['flt'];
     }
 
@@ -269,11 +274,12 @@ class Zend_Amf_Util_BinaryStream
      */
     public function writeDouble($stream)
     {
-        $stream = pack("d", $stream);
-        if ($this->_bigEndian) {
+        $stream = pack('d', $stream);
+        if (!$this->_bigEndian) {
             $stream = strrev($stream);
         }
-        $this->_stream .= $stream;
+        $this->_stream.= $stream;
         return $this;
     }
+
 }