Thrift library updates, remove unsigned types


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664772 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/php/src/protocol/TBinaryProtocol.php b/lib/php/src/protocol/TBinaryProtocol.php
index 0d2349e..4bb0297 100644
--- a/lib/php/src/protocol/TBinaryProtocol.php
+++ b/lib/php/src/protocol/TBinaryProtocol.php
@@ -1,7 +1,7 @@
 <?php
 
 /** For transport operations */
-require_once PREFIX.'thrift/transport/TTransport.php';
+require_once $GLOBALS['THRIFT_ROOT'].'/transport/TTransport.php';
 
 /**
  * Binary implementation of the Thrift protocol.
@@ -12,10 +12,10 @@
 class TBinaryProtocol extends TProtocol {
 
   public function writeMessageBegin($out, $name, $type, $seqid) {
-      return 
-        $this->writeString($out, $name) +
-        $this->writeByte($out, $type) +
-        $this->writeU32($out, $seqid);
+    return 
+      $this->writeString($out, $name) +
+      $this->writeByte($out, $type) +
+      $this->writeI32($out, $seqid);
   }
 
   public function writeMessageEnd($out) {
@@ -88,12 +88,6 @@
     return 1;
   }
 
-  public function writeI08($out, $value) {
-    $data = pack('c', $value);
-    $out->write($data, 1);
-    return 1;
-  }
-
   public function writeI16($out, $value) {
     $data = pack('n', $value);
     $out->write($data, 2);
@@ -107,25 +101,23 @@
   }
 
   public function writeI64($out, $value) {
-
-    /* If we are on a 32bit architecture we have to explicitly deal with 64-bit twos-complement arithmetic
-       since PHP wants to treat all ints as signed and any int over 2^31 - 1 as a float */
-    
-    if(PHP_INT_SIZE == 4) {
-
+    // If we are on a 32bit architecture we have to explicitly deal with
+    // 64-bit twos-complement arithmetic since PHP wants to treat all ints
+    // as signed and any int over 2^31 - 1 as a float   
+    if (PHP_INT_SIZE == 4) {
       $neg = $value < 0;
-  
-      if($neg) {
-	$value*= -1;
+
+      if ($neg) {
+	$value *= -1;
       }
-    
+   
       $hi = (int)($value / 4294967296);
       $lo = (int)$value;
     
-      if($neg) {
+      if ($neg) {
 	$hi = ~$hi;
 	$lo = ~$lo;
-	if(($lo & (int)0xffffffff) == (int)0xffffffff) {
+	if (($lo & (int)0xffffffff) == (int)0xffffffff) {
 	  $lo = 0;
 	  $hi++;
 	} else {
@@ -144,56 +136,18 @@
     return 8;
   }
 
-  public function writeU08($out, $value) {
-    $data = pack('c', $value);
-    $out->write($data, 1);
-    return 1;
-  }
-
-  public function writeU16($out, $value) {
-    $data = pack('n', $value);
-    $out->write($data, 2);
-    return 2;
-  }
-
-  public function writeU32($out, $value) {
-    $data = pack('N', $value);
-    $out->write($data, 4);
-    return 4;
-  }
-
-  public function writeU64($out, $value) {
-
-    /* If we are on a 32bit architecture we have to explicitly deal with 64-bit twos-complement arithmetic
-     since PHP wants to treat all ints as signed and any int over 2^31 - 1 as a float */
-
-    if(PHP_INT_SIZE == 4) {
-
-      $hi = (int)($value / 4294967296);
-      $lo = (int)$value;
-      $data = pack('N2', $hi, $lo);
-    
-    } else {
-      $hi = $value >> 32;
-      $lo = $value & 0xFFFFFFFF;
-      $data = pack('N2', $hi, $lo);
-    }
-    
-    $out->write($data, 8);
-    return 8;
-  }
-
   public function writeString($out, $value) {
     $len = strlen($value);
-    $result = $this->writeU32($out, $len);
+    $result = $this->writeI32($out, $len);
     $out->write($value, $len);
     return $result + $len;
   }
 
   public function readMessageBegin($in, &$name, &$type, &$seqid) {
-      $result = $this->readString($in, $name);
-      $result+= $this->readByte($in, $type);
-      $result+= $this->readU32($in, $seqid);
+    return 
+      $this->readString($in, $name) +
+      $this->readByte($in, $type) +
+      $this->readI32($in, $seqid);
   }
 
   public function readMessageEnd($out) {
@@ -224,10 +178,10 @@
   }
 
   public function readMapBegin($in, &$keyType, &$valType, &$size) {
-    $result = $this->readByte($in, $keyType);
-    $result += $this->readByte($in, $valType);
-    $result += $this->readI32($in, $size);
-    return $result;
+    return
+      $this->readByte($in, $keyType) +
+      $this->readByte($in, $valType) +
+      $this->readI32($in, $size);
   }
 
   public function readMapEnd($in) {
@@ -235,9 +189,9 @@
   }
 
   public function readListBegin($in, &$elemType, &$size) {
-    $result = $this->readByte($in, $elemType);
-    $result += $this->readI32($in, $size);
-    return $result;
+    return
+      $this->readByte($in, $elemType) +
+      $this->readI32($in, $size);
   }
 
   public function readListEnd($in) {
@@ -245,9 +199,9 @@
   }
 
   public function readSetBegin($in, &$elemType, &$size) {
-    $result = $this->readByte($in, $elemType);
-    $result += $this->readI32($in, $size);
-    return $result;
+    return
+      $this->readByte($in, $elemType) +
+      $this->readI32($in, $size);
   }
 
   public function readSetEnd($in) {
@@ -268,19 +222,12 @@
     return 1;
   }
 
-  public function readI08($in, &$value) {
-    $data = $in->readAll(1);
-    $arr = unpack('c', $data);
-    $value = $arr[1];
-    return 1;
-  }
-
   public function readI16($in, &$value) {
     $data = $in->readAll(2);
     $arr = unpack('n', $data);
     $value = $arr[1];
-    if($value > 0x7fff) {
-        $value = 0 - (($value - 1) ^ 0xffff);
+    if ($value > 0x7fff) {
+      $value = 0 - (($value - 1) ^ 0xffff);
     }
     return 2;
   }
@@ -289,33 +236,32 @@
     $data = $in->readAll(4);
     $arr = unpack('N', $data);
     $value = $arr[1];
-    if($value > 0x7fffffff) {
+    if ($value > 0x7fffffff) {
       $value = 0 - (($value - 1) ^ 0xffffffff);
     }
     return 4;
   }
 
   public function readI64($in, &$value) {
-
     $data = $in->readAll(8);
 
     $arr = unpack('N2', $data);
     
-    /* If we are on a 32bit architecture we have to explicitly deal with 64-bit twos-complement arithmetic
-     since PHP wants to treat all ints as signed and any int over 2^31 - 1 as a float */
-
-    if(PHP_INT_SIZE == 4) {
+    // If we are on a 32bit architecture we have to explicitly deal with
+    // 64-bit twos-complement arithmetic since PHP wants to treat all ints
+    // as signed and any int over 2^31 - 1 as a float
+    if (PHP_INT_SIZE == 4) {
 
       $hi = $arr[1];
       $lo = $arr[2];
       $isNeg = $hi  < 0;
     
       // Check for a negative
-      if($isNeg) {
+      if ($isNeg) {
 	$hi = ~$hi & (int)0xffffffff;
 	$lo = ~$lo & (int)0xffffffff;
 
-	if($lo == (int)0xffffffff) {
+	if ($lo == (int)0xffffffff) {
 	  $hi++;
 	  $lo = 0;
 	} else {
@@ -323,22 +269,22 @@
 	}
       }
 
-      /* Force 32bit words in excess of 2G to pe positive - we deal wigh sign
-       explicitly below */
+      // Force 32bit words in excess of 2G to pe positive - we deal wigh sign
+      // explicitly below
       
-      if($hi & (int)0x80000000) {
-	$hi&= (int)0x7fffffff;
+      if ($hi & (int)0x80000000) {
+	$hi &= (int)0x7fffffff;
 	$hi += 0x80000000;
       }
       
-      if($lo & (int)0x80000000) {
-	$lo&= (int)0x7fffffff;
+      if ($lo & (int)0x80000000) {
+	$lo &= (int)0x7fffffff;
 	$lo += 0x80000000;
       }
     
       $value = $hi * 4294967296 + $lo;
 
-      if($isNeg) {
+      if ($isNeg) {
 	$value = 0 - $value;
       }
     } else {
@@ -356,62 +302,8 @@
     return 8;
   }
 
-  public function readU08($in, &$value) {
-    $data = $in->readAll(1);
-    $arr = unpack('c', $data);
-    $value = $arr[1];
-    return 1;
-  }
-
-  public function readU16($in, &$value) {
-    $data = $in->readAll(2);
-    $arr = unpack('n', $data);
-    $value = $arr[1];
-    return 2;
-  }
-
-  public function readU32($in, &$value) {
-    $data = $in->readAll(4);
-    $arr = unpack('N', $data);
-    $value = $arr[1];
-    return 4;
-  }
-
-  public function readU64($in, &$value) {
-    $data = $in->readAll(8);
-    $arr = unpack('N2', $data);
-
-    /* If we are on a 32bit architecture we have to explicitly deal with 64-bit twos-complement arithmetic
-     since PHP wants to treat all ints as signed and any int over 2^31 - 1 as a float */
-
-    if(PHP_INT_SIZE == 4) {
-
-      $hi = $arr[1];
-      $lo = $arr[2];
-
-      /* Prevent implicit integer sign extension */
-    
-      if($hi & (int)0x80000000) {
-	$hi&= (int)0x7fffffff;
-	$hi += 0x80000000;
-      }
-
-      if($lo & (int)0x80000000) {
-	$lo&= (int)0x7fffffff;
-	$lo += 0x80000000;
-      }
-    
-      $value = $hi * 4294967296 + $lo;
-
-    } else {
-
-    $value = $arr[1]*4294967296 + $arr[2];
-    }
-    return 8;
-  }
-
   public function readString($in, &$value) {
-    $result = $this->readU32($in, $len);
+    $result = $this->readI32($in, $len);
     $value = $in->readAll($len);
     return $result + $len;
   }
diff --git a/lib/php/src/protocol/TProtocol.php b/lib/php/src/protocol/TProtocol.php
index a3596ab..15938df 100644
--- a/lib/php/src/protocol/TProtocol.php
+++ b/lib/php/src/protocol/TProtocol.php
@@ -1,7 +1,10 @@
 <?php
 
-/** Types */
-require_once PREFIX.'thrift/protocol/TType.php';
+/**
+ * For Type Constants
+ */
+require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TType.php';
+
 
 /**
  * Protocol module.
@@ -11,19 +14,21 @@
  */
 abstract class TProtocol {
 
-  /** Writes the message header
-  
-      @param TTransport $out Output transport
-      @param name $name Function name
-      @param type $type message type TMessageType::CALL or TMessageType::REPLY
-      @parem seqid $seqid The sequence id of this message */
-
+  /** 
+   * Writes the message header
+   *
+   * @param TTransport $out Output transport
+   * @param string $name Function name
+   * @param int $type message type TMessageType::CALL or TMessageType::REPLY
+   * @param int $seqid The sequence id of this message
+   */
   public abstract function writeMessageBegin($out, $name, $type, $seqid);
 
-  /** Close the message
-  
-      @param TTransport $out Output transport */
-
+  /**
+   * Close the message
+   *
+   * @param TTransport $out Output transport
+   */
   public abstract function writeMessageEnd($out);
 
   /**
@@ -76,6 +81,8 @@
   
   public abstract function writeByte($out, $byte);
   
+  public abstract function writeI16($out, $i16);
+
   public abstract function writeI32($out, $i32);
 
   public abstract function writeI64($out, $i64);
@@ -83,19 +90,21 @@
   public abstract function writeString($out, $str);
 
 
-  /** Reads the message header
-  
-      @param TTransport $out Output transport
-      @param name $name Function name
-      @param type $type message type TMessageType::CALL or TMessageType::REPLY
-      @parem seqid $seqid The sequence id of this message */
-
+  /**
+   * Reads the message header
+   *
+   * @param TTransport $out Output transport
+   * @param string $name Function name
+   * @param int $type message type TMessageType::CALL or TMessageType::REPLY
+   * @parem int $seqid The sequence id of this message
+   */
   public abstract function readMessageBegin($out, &$name, &$type, &$seqid);
 
-  /** Read the close of message
-  
-      @param TTransport $out Output transport */
-
+  /**
+   * Read the close of message
+   *
+   * @param TTransport $out Output transport
+   */
   public abstract function readMessageEnd($out);
 
   public abstract function readStructBegin($in, &$name);
@@ -120,16 +129,27 @@
 
   public abstract function readByte($in, &$byte);
   
+  public abstract function readI16($in, &$i16);
+
   public abstract function readI32($in, &$i32);
 
   public abstract function readI64($in, &$i64);
 
   public abstract function readString($in, &$str);
 
+  /**
+   * The skip function is a utility to parse over unrecognized date without
+   * causing corruption.
+   *
+   * @param TTransport $in Input transport
+   * @param TType $type What type is it
+   */
   public function skip($in, $type) {
     switch ($type) {
     case TType::BYTE:
       return $this->readByte($in, $byte);
+    case TType::I16;
+      return $this->readI16($in, $i16);
     case TType::I32:
       return $this->readI32($in, $i32);
     case TType::I64:
diff --git a/lib/php/src/protocol/TType.php b/lib/php/src/protocol/TType.php
index e9576f9..22c1c3d 100644
--- a/lib/php/src/protocol/TType.php
+++ b/lib/php/src/protocol/TType.php
@@ -6,30 +6,35 @@
  * @package thrift.protocol
  * @author Mark Slee <mcslee@facebook.com>
  */
+
+/**
+ * Data types that can be sent via Thrift
+ */
 class TType {
   const STOP = 0;
   const VOID = 1;
   const BOOL = 2;
   const BYTE = 3;
-  const U08 = 3;
   const I08 = 4;
-  const U16 = 5;
   const I16 = 6;
-  const U32 = 7;
   const I32 = 8;
-  const U64 = 9;
   const I64 = 10;
   const STRING = 11;
   const UTF7 = 11;
   const STRUCT = 12;
   const MAP = 13;
   const SET = 14;
-  const LST = 15; // cannot use LIST keyword in PHP!
+  const LST = 15;    // N.B. cannot use LIST keyword in PHP!
   const UTF8 = 16;
   const UTF16 = 17;
 }
 
+/**
+ * Message types for RPC
+ */
 class TMessageType {
-      const CALL  = 1;
-      const REPLY  = 2;
+  const CALL  = 1;
+  const REPLY  = 2;
 }
+
+?>