Commits

Anonymous committed c3bcbad

decoding binary seems working.

Comments (0)

Files changed (2)

       (* see http://www.erlang.org/doc/apps/erts/erl_ext_dist.html for details*)
     begin match (String.get local_bin 0) with
       | 'a' ->  (* SMALL_INTEGER_EXT *)
-	  Ok( Int(int_of_char (String.get local_bin 1)), Str.string_after local_bin 1)
+	  Ok( Int(int_of_char (String.get local_bin 1)), Str.string_after local_bin 2)
       | 'b' ->  (* INTEGER_EXT *)
-	  Ok( Int( make_int32 (String.sub local_bin 1 5)), Str.string_after local_bin 5);
+	  Ok( Int( make_int32 (String.sub local_bin 1 5)), Str.string_after local_bin 6);
       | 'c' -> (* FLOAT_EXT *)
 	  let v = Scanf.sscanf (String.sub local_bin 1 32) "%f" (fun x->x) in
 	    Ok( Float( v ), Str.string_after local_bin 32); 
 	  None;
       | 'k' -> (* STRING_EXT *)
 	  let len = (int_of_char (String.get local_bin 1))*256 + (int_of_char (String.get local_bin 2)) in
-	    Ok( String( String.sub local_bin 3 (len+3) ), Str.string_after local_bin (len+3) );
+	    Ok( String( String.sub local_bin 3 len ), Str.string_after local_bin (len+3) );
       | 'l' -> (* LIST_EXT *)
 	  let len = make_int32 (String.sub local_bin 1 5) in
 	    decode_list len (Str.string_after local_bin 5) [];
       | 'n' -> (* SMALL_BIG_EXT *)
 	  let n = int_of_char (String.get local_bin 1) in
 	  let sign = int_of_char  (String.get local_bin 2) in
-	  let result = make_small_bigint (String.sub local_bin 3 (n+3)) n in
+	  let result = make_small_bigint (String.sub local_bin 3 n) n in
 	    if sign=0 then 
 	      Ok( Int(result), Str.string_after local_bin (n+3) )
 	    else if sign=1 then
       | 'o' -> (* LARGE_BIG_EXT *)
 	  let n = make_int32 local_bin in
 	  let sign = int_of_char  (String.get local_bin 5) in
-	  let result = make_small_bigint (String.sub local_bin 6 (n+6)) n in
+	  let result = make_small_bigint (String.sub local_bin 6 n) n in
 	    if sign=0 then 
 	      Ok( Int(result), Str.string_after local_bin (n+6) )
 	    else if sign=1 then
     end in
     begin match int_of_char (String.get bin 0) with
       | 131 -> (* magic number for BERT term; *)
-	  local_decode (String.sub bin 1 ((String.length bin)-1)); 
+	  local_decode (Str.string_after bin 1);
       | _ -> raise Unknown_type;
     end;;
 
 	  print_termlist ']' list; 
 	end;
       | String(str)->
+	  print_char '"';
 	  print_string str;
+	  print_char '"';
       | Binary(bin)-> begin
 	  print_binary bin ;
 	end;
     let fch = open_in_bin filename in (* opening file channel *)
       load fch ""
   in
+  let rec decode_all bin = 
+    begin match Bert.decode_binary bin with
+      | Bert.Ok(term, remain)->
+	  Bert.print_erlterm term;
+	  print_endline "";
+	  if String.length remain > 0 then 
+	    decode_all remain
+	  else
+	    print_endline "==all read.";
+      | _->
+	  print_endline "==failed in decoding.";
+    end in
   let bin = load_file filename in
-  let result = Bert.decode_binary bin in
     Printf.printf "reading %s: \n" filename;
-    begin match result with
-      | Bert.Ok(term, remain)->
-	  Bert.print_binary_string bin;
-	  Bert.print_erlterm term;
-	  print_endline "\n==all read.";
-      | _->
-	  print_endline "\n==failed in decoding.";
-    end;;
+    Bert.print_binary_string bin;
+    decode_all bin;;
 
 let _ = 
   let argv = Array.to_list Sys.argv in