Thrift: OCaml library binary protocol fix

Summary: Binary protocol wasn't dealing with messages properly. Also
there was a math bug in readI32.
Reviewed by: mcslee
Test plan: Yes
Revert plan: yes


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665171 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/ocaml/src/TBinaryProtocol.ml b/lib/ocaml/src/TBinaryProtocol.ml
index 44433d6..748423f 100644
--- a/lib/ocaml/src/TBinaryProtocol.ml
+++ b/lib/ocaml/src/TBinaryProtocol.ml
@@ -11,13 +11,12 @@
 
 
 let comp_int b n = 
-  let s = ref 0 in
-  let sb = Sys.word_size - 8*n in
+  let s = ref 0l in
+  let sb = 32 - 8*n in
     for i=0 to (n-1) do
-      s:=!s lor ((int_of_char b.[i]) lsl (8*(n-1-i)))
+      s:= Int32.logor !s (Int32.shift_left (Int32.of_int (int_of_char b.[i])) (8*(n-1-i)))
     done;
-    s:=(!s lsl sb) asr sb;
-    !s
+    Int32.to_int (Int32.shift_right (Int32.shift_left !s sb) sb)
 
 let comp_int64 b n =
   let s = ref 0L in
@@ -26,6 +25,9 @@
     done;
     !s
 
+let version_mask = 0xffff0000
+let version_1 = 0x80010000
+
 class t trans =
 object (self)
   inherit P.t trans
@@ -61,8 +63,8 @@
       trans#write s 0 n
   method writeBinary a = self#writeString a
   method writeMessageBegin (n,t,s) =
+    self#writeI32 (version_1 lor (P.message_type_to_i t));
     self#writeString n;
-    self#writeByte (P.message_type_to_i t);
     self#writeI32 s
   method writeMessageEnd = ()
   method writeStructBegin s = ()
@@ -109,9 +111,14 @@
       buf
   method readBinary = self#readString
   method readMessageBegin =
-    let s = self#readString in
-    let mt = P.message_type_of_i (self#readByte) in
-      (s,mt, self#readI32)
+    let ver = self#readI32 in
+      if (ver land version_mask != version_1) then
+        (print_int ver;
+        raise (P.TProtocolExn (P.BAD_VERSION, "Missing version identifier")))
+      else
+        let s = self#readString in
+        let mt = P.message_type_of_i (ver land 0xFF) in
+          (s,mt, self#readI32)
   method readMessageEnd = ()
   method readStructBegin =
     ""
diff --git a/lib/ocaml/src/Thrift.ml b/lib/ocaml/src/Thrift.ml
index 224febb..8ff5fa9 100644
--- a/lib/ocaml/src/Thrift.ml
+++ b/lib/ocaml/src/Thrift.ml
@@ -252,6 +252,15 @@
   object
     method virtual getProtocol : Transport.t -> t
   end
+
+  type exn_type =
+      | UNKNOWN
+      | INVALID_DATA
+      | NEGATIVE_SIZE
+      | SIZE_LIMIT
+      | BAD_VERSION
+
+  exception TProtocolExn of exn_type * string;;
            
 end;;