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/cpp/src/Thrift.h b/lib/cpp/src/Thrift.h
index 1f98d82..9254b56 100644
--- a/lib/cpp/src/Thrift.h
+++ b/lib/cpp/src/Thrift.h
@@ -9,7 +9,7 @@
 #include <set>
 #include <exception>
 
-namespace facebook {namespace thrift {
+namespace facebook { namespace thrift {
 
 class Exception : public std::exception {
 private:
diff --git a/lib/cpp/src/protocol/TBinaryProtocol.cc b/lib/cpp/src/protocol/TBinaryProtocol.cc
index f2fbe87..51c9306 100644
--- a/lib/cpp/src/protocol/TBinaryProtocol.cc
+++ b/lib/cpp/src/protocol/TBinaryProtocol.cc
@@ -7,11 +7,11 @@
 uint32_t TBinaryProtocol::writeMessageBegin(shared_ptr<TTransport> out,
 					    const std::string name,
 					    const TMessageType messageType,
-					    const uint32_t seqid) const {
+					    const int32_t seqid) const {
   return 
     writeString(out, name) + 
-    writeByte(out, (uint8_t)messageType) +
-    writeU32(out, seqid);
+    writeByte(out, (int8_t)messageType) +
+    writeI32(out, seqid);
 }
 
 uint32_t TBinaryProtocol::writeMessageEnd(shared_ptr<TTransport> out) const {
@@ -32,7 +32,7 @@
                                           const TType fieldType,
                                           const int16_t fieldId) const {
   return
-    writeByte(out, (uint8_t)fieldType) +
+    writeByte(out, (int8_t)fieldType) +
     writeI16(out, fieldId);
 }
 
@@ -42,7 +42,7 @@
 
 uint32_t TBinaryProtocol::writeFieldStop(shared_ptr<TTransport> out) const {
   return
-    writeByte(out, (uint8_t)T_STOP);
+    writeByte(out, (int8_t)T_STOP);
 }  
                                
 uint32_t TBinaryProtocol::writeMapBegin(shared_ptr<TTransport> out,
@@ -50,9 +50,9 @@
                                         const TType valType,
                                         const uint32_t size) const {
   return
-    writeByte(out, (uint8_t)keyType) +
-    writeByte(out, (uint8_t)valType) +
-    writeU32(out, (uint32_t)size);
+    writeByte(out, (int8_t)keyType) +
+    writeByte(out, (int8_t)valType) +
+    writeI32(out, (int32_t)size);
 }
 
 uint32_t TBinaryProtocol::writeMapEnd(shared_ptr<TTransport> out) const {
@@ -63,8 +63,8 @@
                                          const TType elemType,
                                          const uint32_t size) const {
   return
-    writeByte(out, (uint8_t) elemType) +
-    writeU32(out, (int32_t)size);
+    writeByte(out, (int8_t) elemType) +
+    writeI32(out, (int32_t)size);
 }
 
 uint32_t TBinaryProtocol::writeListEnd(shared_ptr<TTransport> out) const {
@@ -75,8 +75,8 @@
                                         const TType elemType,
                                         const uint32_t size) const {
   return
-    writeByte(out, (uint8_t)elemType) +
-    writeU32(out, (int32_t)size);
+    writeByte(out, (int8_t)elemType) +
+    writeI32(out, (int32_t)size);
 }
 
 uint32_t TBinaryProtocol::writeSetEnd(shared_ptr<TTransport> out) const {
@@ -91,18 +91,11 @@
 }
 
 uint32_t TBinaryProtocol::writeByte(shared_ptr<TTransport> out,
-                                    const uint8_t byte) const {
-  out->write(&byte, 1);
+                                    const int8_t byte) const {
+  out->write((uint8_t*)&byte, 1);
   return 1;
 }
 
-uint32_t TBinaryProtocol::writeU16(shared_ptr<TTransport> out,
-                                   const uint16_t u16) const {
-  uint16_t net = (uint16_t)htons(u16);
-  out->write((uint8_t*)&net, 2);
-  return 2;
-}
-
 uint32_t TBinaryProtocol::writeI16(shared_ptr<TTransport> out,
                                    const int16_t i16) const {
   int16_t net = (int16_t)htons(i16);
@@ -110,13 +103,6 @@
   return 2;
 }
 
-uint32_t TBinaryProtocol::writeU32(shared_ptr<TTransport> out,
-                                   const uint32_t u32) const {
-  uint32_t net = (uint32_t)htonl(u32);
-  out->write((uint8_t*)&net, 4);
-  return 4;
-}
-
 uint32_t TBinaryProtocol::writeI32(shared_ptr<TTransport> out,
                                    const int32_t i32) const {
   int32_t net = (int32_t)htonl(i32);
@@ -124,13 +110,6 @@
   return 4;
 }
 
-uint32_t TBinaryProtocol::writeU64(shared_ptr<TTransport> out,
-                                   const uint64_t u64) const {
-  uint64_t net = (uint64_t)htonll(u64);
-  out->write((uint8_t*)&net, 8);
-  return 8;
-}
-
 uint32_t TBinaryProtocol::writeI64(shared_ptr<TTransport> out,
                                    const int64_t i64) const {
   int64_t net = (int64_t)htonll(i64);
@@ -152,14 +131,14 @@
 uint32_t TBinaryProtocol::readMessageBegin(shared_ptr<TTransport> in,
 					   std::string& name,
 					   TMessageType& messageType,
-					   uint32_t& seqid) const {
+					   int32_t& seqid) const {
 
   uint32_t result = 0;
-  uint8_t type;
+  int8_t type;
   result+= readString(in, name);
   result+=  readByte(in, type);
   messageType = (TMessageType)type;
-  result+= readU32(in, seqid);
+  result+= readI32(in, seqid);
   return result;
 }
 
@@ -182,7 +161,7 @@
                                          TType& fieldType,
                                          int16_t& fieldId) const {
   uint32_t result = 0;
-  uint8_t type;
+  int8_t type;
   result += readByte(in, type);
   fieldType = (TType)type;
   if (fieldType == T_STOP) {
@@ -201,13 +180,16 @@
                                        TType& keyType,
                                        TType& valType,
                                        uint32_t& size) const {
-  uint8_t k, v;
+  int8_t k, v;
   uint32_t result = 0;
+  int32_t sizei;
   result += readByte(in, k);
   keyType = (TType)k;
   result += readByte(in, v);
   valType = (TType)v;
-  result += readU32(in, size);
+  result += readI32(in, sizei);
+  // TODO(mcslee): check for negative size
+  size = (uint32_t)sizei;
   return result;
 }
 
@@ -218,11 +200,14 @@
 uint32_t TBinaryProtocol::readListBegin(shared_ptr<TTransport> in,
                                         TType& elemType,
                                         uint32_t& size) const {
-  uint8_t e;
+  int8_t e;
   uint32_t result = 0;
+  int32_t sizei;
   result += readByte(in, e);
   elemType = (TType)e;
-  result += readU32(in, size);
+  result += readI32(in, sizei);
+  // TODO(mcslee): check for negative size
+  size = (uint32_t)sizei;
   return result;
 }
 
@@ -233,11 +218,14 @@
 uint32_t TBinaryProtocol::readSetBegin(shared_ptr<TTransport> in,
                                        TType& elemType,
                                        uint32_t& size) const {
-  uint8_t e;
+  int8_t e;
   uint32_t result = 0;
+  int32_t sizei;
   result += readByte(in, e);
   elemType = (TType)e;
-  result += readU32(in, size);
+  result += readI32(in, sizei);
+  // TODO(mcslee): check for negative size
+  size = (uint32_t)sizei;
   return result;
 }
 
@@ -249,27 +237,18 @@
                                    bool& value) const {
   uint8_t b[1];
   in->readAll(b, 1);
-  value = *(uint8_t*)b != 0;
+  value = *(int8_t*)b != 0;
   return 1;
 }
 
 uint32_t TBinaryProtocol::readByte(shared_ptr<TTransport> in,
-                                   uint8_t& byte) const {
+                                   int8_t& byte) const {
   uint8_t b[1];
   in->readAll(b, 1);
-  byte = *(uint8_t*)b;
+  byte = *(int8_t*)b;
   return 1;
 }
 
-uint32_t TBinaryProtocol::readU16(shared_ptr<TTransport> in,
-                                  uint16_t& u16) const {
-  uint8_t b[2];
-  in->readAll(b, 2);
-  u16 = *(uint16_t*)b;
-  u16 = (uint16_t)ntohs(u16);
-  return 2;
-}
-
 uint32_t TBinaryProtocol::readI16(shared_ptr<TTransport> in,
                                   int16_t& i16) const {
   uint8_t b[2];
@@ -279,15 +258,6 @@
   return 2;
 }
 
-uint32_t TBinaryProtocol::readU32(shared_ptr<TTransport> in,
-                                  uint32_t& u32) const {
-  uint8_t b[4];
-  in->readAll(b, 4);
-  u32 = *(uint32_t*)b;
-  u32 = (uint32_t)ntohl(u32);
-  return 4;
-}
-
 uint32_t TBinaryProtocol::readI32(shared_ptr<TTransport> in,
                                   int32_t& i32) const {
   uint8_t b[4];
@@ -297,15 +267,6 @@
   return 4;
 }
 
-uint32_t TBinaryProtocol::readU64(shared_ptr<TTransport> in,
-                                  uint64_t& u64) const {
-  uint8_t b[8];
-  in->readAll(b, 8);
-  u64 = *(uint64_t*)b;
-  u64 = (uint64_t)ntohll(u64);
-  return 8;
-}
-
 uint32_t TBinaryProtocol::readI64(shared_ptr<TTransport> in,
                                   int64_t& i64) const {
   uint8_t b[8];
@@ -318,8 +279,10 @@
 uint32_t TBinaryProtocol::readString(shared_ptr<TTransport> in,
                                      string& str) const {
   uint32_t result;
-  uint32_t size;
-  result = readU32(in, size);
+  int32_t size;
+  result = readI32(in, size);
+
+  // TODO(mcslee): check for negative size
 
   // Use the heap here to prevent stack overflow for v. large strings
   uint8_t *b = new uint8_t[size];
@@ -329,4 +292,5 @@
 
   return result + (uint32_t)size;
 }
+
 }}} // facebook::thrift::protocol
diff --git a/lib/cpp/src/protocol/TBinaryProtocol.h b/lib/cpp/src/protocol/TBinaryProtocol.h
index 7780f16..d66fa2f 100644
--- a/lib/cpp/src/protocol/TBinaryProtocol.h
+++ b/lib/cpp/src/protocol/TBinaryProtocol.h
@@ -27,7 +27,7 @@
   virtual uint32_t writeMessageBegin(shared_ptr<TTransport> out,
 				     const std::string name,
 				     const TMessageType messageType,
-				     const uint32_t seqid) const;
+				     const int32_t seqid) const;
 
   virtual uint32_t writeMessageEnd(shared_ptr<TTransport> out) const;
 
@@ -69,23 +69,14 @@
 		      const bool value) const;
 
   uint32_t writeByte(shared_ptr<TTransport> out,
-		      const uint8_t byte) const;
-
-  uint32_t writeU16(shared_ptr<TTransport> out,
-		    const uint16_t u16) const;
+		      const int8_t byte) const;
 
   uint32_t writeI16(shared_ptr<TTransport> out,
 		     const int16_t i16) const;
 
-  uint32_t writeU32(shared_ptr<TTransport> out,
-		     const uint32_t u32) const;
-
   uint32_t writeI32(shared_ptr<TTransport> out,
 		     const int32_t i32) const;
 
-  uint32_t writeU64(shared_ptr<TTransport> out,
-		     const uint64_t u64) const;
-
   uint32_t writeI64(shared_ptr<TTransport> out,
 		     const int64_t i64) const;
 
@@ -100,7 +91,7 @@
   uint32_t readMessageBegin(shared_ptr<TTransport> in,
 			    std::string& name,
 			    TMessageType& messageType,
-			    uint32_t& seqid) const;
+			    int32_t& seqid) const;
 
   uint32_t readMessageEnd(shared_ptr<TTransport> in) const;
 
@@ -139,23 +130,14 @@
 		      bool& value) const;
 
   uint32_t readByte(shared_ptr<TTransport> in,
-		     uint8_t& byte) const;
-
-  uint32_t readU16(shared_ptr<TTransport> in,
-		    uint16_t& u16) const;
+                    int8_t& byte) const;
 
   uint32_t readI16(shared_ptr<TTransport> in,
 		    int16_t& i16) const;
 
-  uint32_t readU32(shared_ptr<TTransport> in,
-		    uint32_t& u32) const;
-
   uint32_t readI32(shared_ptr<TTransport> in,
 		    int32_t& i32) const;
 
-  uint32_t readU64(shared_ptr<TTransport> in,
-		    uint64_t& u64) const;
-
   uint32_t readI64(shared_ptr<TTransport> in,
 		    int64_t& i64) const;
 
diff --git a/lib/cpp/src/protocol/TProtocol.h b/lib/cpp/src/protocol/TProtocol.h
index 092dd8d..2b3666f 100644
--- a/lib/cpp/src/protocol/TProtocol.h
+++ b/lib/cpp/src/protocol/TProtocol.h
@@ -33,11 +33,8 @@
   T_VOID       = 1,
   T_BOOL       = 2,
   T_BYTE       = 3,
-  T_U08        = 3,
-  T_I08        = 4,
-  T_U16        = 5,
+  T_I08        = 3,
   T_I16        = 6,
-  T_U32        = 7,
   T_I32        = 8,
   T_U64        = 9,
   T_I64        = 10,
@@ -82,7 +79,7 @@
   virtual uint32_t writeMessageBegin(shared_ptr<TTransport> out,
 				     const std::string name,
 				     const TMessageType messageType,
-				     const uint32_t seqid) const = 0;
+				     const int32_t seqid) const = 0;
 
   virtual uint32_t writeMessageEnd(shared_ptr<TTransport> out) const = 0;
 
@@ -124,23 +121,14 @@
 			     const bool value) const = 0;
 
   virtual uint32_t writeByte(shared_ptr<TTransport> out,
-			     const uint8_t byte) const = 0;
+			     const int8_t byte) const = 0;
 
   virtual uint32_t writeI16(shared_ptr<TTransport> out,
 			    const int16_t i16) const = 0;
 
-  virtual uint32_t writeU16(shared_ptr<TTransport> out,
-			    const uint16_t u16) const = 0;
-
-  virtual uint32_t writeU32(shared_ptr<TTransport> out,
-			    const uint32_t u32) const = 0;
-  
   virtual uint32_t writeI32(shared_ptr<TTransport> out,
 			    const int32_t i32) const = 0;
 
-  virtual uint32_t writeU64(shared_ptr<TTransport> out,
-			    const uint64_t u64) const = 0;
-
   virtual uint32_t writeI64(shared_ptr<TTransport> out,
 			    const int64_t i64) const = 0;
 
@@ -154,7 +142,7 @@
   virtual uint32_t readMessageBegin(shared_ptr<TTransport> in,
 				    std::string& name,
 				    TMessageType& messageType,
-				    uint32_t& seqid) const = 0;
+				    int32_t& seqid) const = 0;
   
   virtual uint32_t readMessageEnd(shared_ptr<TTransport> in) const = 0;
 
@@ -193,23 +181,14 @@
 			    bool& value) const = 0;
 
   virtual uint32_t readByte(shared_ptr<TTransport> in,
-			    uint8_t& byte) const = 0;
-
-  virtual uint32_t readU16(shared_ptr<TTransport> in,
-			   uint16_t& u16) const = 0;
+			    int8_t& byte) const = 0;
 
   virtual uint32_t readI16(shared_ptr<TTransport> in,
 			   int16_t& i16) const = 0;
 
-  virtual uint32_t readU32(shared_ptr<TTransport> in,
-			   uint32_t& u32) const = 0;
-
   virtual uint32_t readI32(shared_ptr<TTransport> in,
 			   int32_t& i32) const = 0;
 
-  virtual uint32_t readU64(shared_ptr<TTransport> in,
-			   uint64_t& u64) const = 0;
-
   virtual uint32_t readI64(shared_ptr<TTransport> in,
 			   int64_t& i64) const = 0;
 
@@ -223,24 +202,19 @@
     switch (type) {
     case T_BYTE:
       {
-        uint8_t byte;
+        int8_t byte;
         return readByte(in, byte);
       }
-    case T_U32:
+    case T_I16:
       {
-        uint32_t u32;
-        return readU32(in, u32);
+        int16_t i16;
+        return readI16(in, i16);
       }
     case T_I32:
       {
         int32_t i32;
         return readI32(in, i32);
       }
-    case T_U64:
-      {
-        uint64_t u64;
-        return readU64(in, u64);
-      }
     case T_I64:
       {
         int64_t i64;
diff --git a/lib/php/src/Thrift.php b/lib/php/src/Thrift.php
index 1e8f3fb..ee48092 100644
--- a/lib/php/src/Thrift.php
+++ b/lib/php/src/Thrift.php
@@ -1,5 +1,9 @@
 <?php
 
-include_once PREFIX.'thrift/protocol/TProtocol.php';
+/** Set global THRIFT ROOT automatically via inclusion here */
+if (!isset($GLOBALS['THRIFT_ROOT'])) {
+  $GLOBALS['THRIFT_ROOT'] = dirname(__FILE__);
+}
+include_once $GLOBALS['THRIFT_ROOT'].'/protocol/TProtocol.php';
 
 ?>
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;
 }
+
+?>