Version 0.1.2.
[guestfs-browser.git] / utils.ml
index 02bd7a0..ca2432f 100644 (file)
--- a/utils.ml
+++ b/utils.ml
@@ -88,10 +88,14 @@ let unique = let i = ref 0 in fun () -> incr i; !i
 let mklabel text =
   (GMisc.label ~text () :> GObj.widget)
 
-(* XXX No binding for g_markup_escape in lablgtk2. *)
+(* g_markup_escape is not bound by lablgtk2, but we want to provide
+ * extra protection for \0 characters appearing in the string
+ * anyway.
+ *)
 let markup_escape name =
   let f = function
     | '&' -> "&amp;" | '<' -> "&lt;" | '>' -> "&gt;"
+    | '\000' -> "\\0"
     | c -> String.make 1 c
   in
   String.replace_chars f name
@@ -139,3 +143,62 @@ and is_wo mode =           test_bit 0o002L mode
 and is_xo mode =           test_bit 0o001L mode
 
 and test_bit mask mode = Int64.logand mode mask = mask
+
+let tmpdir () =
+  let chan = open_in "/dev/urandom" in
+  let data = String.create 16 in
+  really_input chan data 0 (String.length data);
+  close_in chan;
+  let data = Digest.to_hex (Digest.string data) in
+  (* Note this is secure, because if the name already exists, even as a
+   * symlink, mkdir(2) will fail.
+   *)
+  let tmpdir = Filename.temp_dir_name // sprintf "febootstrap%s.tmp" data in
+  Unix.mkdir tmpdir 0o700;
+  at_exit
+    (fun () ->
+       let cmd = sprintf "rm -rf %s" (Filename.quote tmpdir) in
+       ignore (Sys.command cmd));
+  tmpdir
+
+(* This would be so much simpler with ChriS's delimited
+ * overloading macro XXX
+ *)
+let i32_of_string_le v =
+  let b0 = int_of_char (String.unsafe_get v 0) in
+  let b1 = int_of_char (String.unsafe_get v 1) in
+  let b2 = int_of_char (String.unsafe_get v 2) in
+  let b3 = Int32.of_int (int_of_char (String.unsafe_get v 3)) in
+  Int32.logor
+    (Int32.of_int (b0 lor (b1 lsl 8) lor (b2 lsl 16)))
+    (Int32.shift_left b3 24)
+
+let i32_of_string_be v =
+  let b0 = Int32.of_int (int_of_char (String.unsafe_get v 0)) in
+  let b1 = int_of_char (String.unsafe_get v 1) in
+  let b2 = int_of_char (String.unsafe_get v 2) in
+  let b3 = int_of_char (String.unsafe_get v 3) in
+  Int32.logor
+    (Int32.of_int (b3 lor (b2 lsl 8) lor (b1 lsl 16)))
+    (Int32.shift_left b0 24)
+
+let i64_of_string_le v =
+  let b0 = int_of_char (String.unsafe_get v 0) in
+  let b1 = int_of_char (String.unsafe_get v 1) in
+  let b2 = int_of_char (String.unsafe_get v 2) in
+  let b3 = Int64.of_int (int_of_char (String.unsafe_get v 3)) in
+  let b4 = Int64.of_int (int_of_char (String.unsafe_get v 4)) in
+  let b5 = Int64.of_int (int_of_char (String.unsafe_get v 5)) in
+  let b6 = Int64.of_int (int_of_char (String.unsafe_get v 6)) in
+  let b7 = Int64.of_int (int_of_char (String.unsafe_get v 7)) in
+  Int64.logor
+    (Int64.logor
+       (Int64.logor
+          (Int64.logor
+             (Int64.logor
+                (Int64.of_int (b0 lor (b1 lsl 8) lor (b2 lsl 16)))
+                (Int64.shift_left b3 24))
+             (Int64.shift_left b4 32))
+          (Int64.shift_left b5 40))
+       (Int64.shift_left b6 48))
+    (Int64.shift_left b7 56)