Commits

UENISHI Kota  committed fa0ce44

encoding binary done .

  • Participants
  • Parent commits 9433b13

Comments (0)

Files changed (2)

     else v
   in make_int32_ bin 0 0;;
 
+let encode_int32 i = 
+    let j = Int32.of_int i in
+    let ff = Int32.of_int 0xFF in
+      ((char_of_int (Int32.to_int (Int32.logand ff (Int32.shift_right j 24)))), 
+       (char_of_int (Int32.to_int (Int32.logand ff (Int32.shift_right j 16)))),
+       (char_of_int (Int32.to_int (Int32.logand ff (Int32.shift_right j 8)))),
+       (char_of_int (Int32.to_int (Int32.logand ff j))) );;
+
 (* binary -> (decoded tuple, remain), (more, Length), (error, Reason)
    throws exceptions: Not_supported | Unknown_type
    when more binaries  *)
 let decode_binary bin = 
-
   (* decoding tuple, traverses the list twice: first when parsing 
      (and pushes each term to stack list), second when reversing *)
   let rec decode_tuple len binary list = 
       2+ofs;
     end
   else if i < 0xFFFFFFFF then (* INTEGER_EXT *)
-    let j = Int32.of_int i in
-    let ff = Int32.of_int 0xFF in
-      begin
+    begin
+      let (a,b,c,d) = encode_int32 i in
 	String.set buf ofs 'b';
-	String.set buf (ofs+1) (char_of_int (Int32.to_int (Int32.logand ff (Int32.shift_right j 24))));
-	String.set buf (ofs+2) (char_of_int (Int32.to_int (Int32.logand ff (Int32.shift_right j 16))));
-	String.set buf (ofs+3) (char_of_int (Int32.to_int (Int32.logand ff (Int32.shift_right j 8))));
-	String.set buf (ofs+4) (char_of_int (Int32.to_int (Int32.logand ff j)));
-      5+ofs;
+	String.set buf (ofs+1) a;
+	String.set buf (ofs+2) b;
+	String.set buf (ofs+3) c;
+	String.set buf (ofs+4) d;
+	5+ofs;
     end
   else (* SMALL_BIG_EXT *) 
     raise Not_supported;;
     String.set tmp_str 1 (char_of_int (len/256));
     String.set tmp_str 2 (char_of_int (len mod 256));
     String.blit tmp_str 0 buf ofs (len+3);
+    ofs + (String.length str);;
+
+let encode_binary bin buf ofs = (* BINARY_EXT *)
+  let str = Printf.sprintf "mabcd%s" bin in
+  let (a,b,c,d) = encode_int32 (String.length bin) in
+    String.set str 1 a;
+    String.set str 2 b;
+    String.set str 3 c;
+    String.set str 4 d;
+    String.blit str 0 buf ofs (String.length str);
     ofs + String.length str;;
 
-let encode_binary bin = 
-  (* BINARY_EXT *) "m";;
-
 let encode_tuple tuple = function
   | Tuple(t) when List.length t < 256 -> (* SMALL_TUPLE_EXT *)
       "h";

File termreader.ml

 	  List.map print_termfile list;;
 
 let _ = 
-  let str = String.create 64 in
+  let str = String.create 128 in
   let _ = Bert.init_buf str in
   let i = Bert.encode_int 774567 str 1 in
   let j = Bert.encode_float 234.354 str i in
   let k = Bert.encode_atom "HAHAHAHA" str j in
   let l = Bert.encode_string "HAHAHAHA" str k in
+  let m = Bert.encode_binary "HAHAHAHA" str l in
     Bert.print_binary_string str;
 (*    print_endline str; *)
-    l;;
+    m;;