Version 0.1.3.
[guestfs-browser.git] / utils.ml
index f9cf35d..95c1289 100644 (file)
--- a/utils.ml
+++ b/utils.ml
@@ -17,6 +17,7 @@
  *)
 
 open ExtString
+open ExtList
 open CamomileLibrary
 open Default.Camomile
 
@@ -176,3 +177,44 @@ let windows_string_to_utf8 str =
     String.sub str 0 (UTF8.last str)
   else
     str
+
+(* Best effort convert hive value to printable string. *)
+let rec printable_hivex_value ?split_long_lines t v =
+  let hex = reg_hex_of_string ?split_long_lines in
+  match t with
+  | Hivex.REG_NONE -> if v = "" then "" else hex v
+  | Hivex.REG_SZ ->
+      (try windows_string_to_utf8 v with _ -> hex v)
+  | Hivex.REG_EXPAND_SZ ->
+      (try windows_string_to_utf8 v with _ -> hex v)
+  | Hivex.REG_BINARY -> hex v
+  | Hivex.REG_DWORD ->
+      (bitmatch Bitstring.bitstring_of_string v with
+       | { i : 32 : littleendian } -> sprintf "%08lx" i
+       | { _ } -> hex v)
+  | Hivex.REG_DWORD_BIG_ENDIAN ->
+      (bitmatch Bitstring.bitstring_of_string v with
+       | { i : 32 : bigendian } -> sprintf "%08lx" i
+       | { _ } -> hex v)
+  | Hivex.REG_LINK -> hex v
+  | Hivex.REG_MULTI_SZ -> (* XXX should be better for this one *)
+      hex v
+  | Hivex.REG_RESOURCE_LIST -> hex v
+  | Hivex.REG_FULL_RESOURCE_DESCRIPTOR -> hex v
+  | Hivex.REG_RESOURCE_REQUIREMENTS_LIST -> hex v
+  | Hivex.REG_QWORD ->
+      (bitmatch Bitstring.bitstring_of_string v with
+       | { i : 64 : littleendian } -> sprintf "%016Lx" i
+       | { _ } -> hex v)
+  | Hivex.REG_UNKNOWN i32 -> hex v
+
+(* Convert binary data to a hex string.  This includes line breaks. *)
+and reg_hex_of_string ?(split_long_lines=false) v =
+  let vs = String.explode v in
+  let vs = List.mapi (
+    fun i c ->
+      sprintf "%s%02x"
+        (if split_long_lines && i mod 16 = 0 then "\n" else "")
+        (int_of_char c)
+  ) vs in
+  String.concat "," vs