Use bitstring, enable display of symlinks on NTFS.
[guestfs-browser.git] / filetree_markup.ml
index 206700b..55bb425 100644 (file)
 
 open ExtString
 open ExtList
-open Unix
-
 open CamomileLibrary
 open Default.Camomile
+open Unix
 
 open Utils
 open Filetree_type
@@ -109,15 +108,13 @@ and markup_of_regvalue ?(visited = false) h value =
       | Hivex.REG_EXPAND_SZ -> markup_windows_string v
       | Hivex.REG_BINARY -> markup_hex_data v
       | Hivex.REG_DWORD ->
-          if len = 4 then
-            sprintf "%08lx" (i32_of_string_le v)
-          else
-            markup_hex_data v
+          (bitmatch Bitstring.bitstring_of_string v with
+           | { i : 32 : littleendian } -> sprintf "%08lx" i
+           | { _ } -> markup_hex_data v)
       | Hivex.REG_DWORD_BIG_ENDIAN ->
-          if len = 4 then
-            sprintf "%08lx" (i32_of_string_be v)
-          else
-            markup_hex_data v
+          (bitmatch Bitstring.bitstring_of_string v with
+           | { i : 32 : bigendian } -> sprintf "%08lx" i
+           | { _ } -> markup_hex_data v)
       | Hivex.REG_LINK -> markup_hex_data v
       | Hivex.REG_MULTI_SZ -> (* XXX could do better with this *)
           markup_hex_data v
@@ -125,10 +122,9 @@ and markup_of_regvalue ?(visited = false) h value =
       | Hivex.REG_FULL_RESOURCE_DESCRIPTOR -> markup_hex_data v
       | Hivex.REG_RESOURCE_REQUIREMENTS_LIST -> markup_hex_data v
       | Hivex.REG_QWORD ->
-          if len = 8 then
-            sprintf "%016Lx" (i64_of_string_le v)
-          else
-            markup_hex_data v
+          (bitmatch Bitstring.bitstring_of_string v with
+           | { i : 64 : littleendian } -> sprintf "%016Lx" i
+           | { _ } -> markup_hex_data v)
       | Hivex.REG_UNKNOWN i32 -> markup_hex_data v
     ) in
 
@@ -147,23 +143,10 @@ and markup_hex_data v =
 
 (* Best guess the format of the string and convert to UTF-8. *)
 and markup_windows_string v =
-  let utf16le = CharEncoding.utf16le in
-  let utf8 = CharEncoding.utf8 in
-  try
-    let v = CharEncoding.recode_string ~in_enc:utf16le ~out_enc:utf8 v in
-    (* Registry strings include the final \0 so remove this if present. *)
-    let len = UTF8.length v in
-    let v =
-      if len > 0 && UChar.code (UTF8.get v (len-1)) = 0 then
-        String.sub v 0 (UTF8.last v)
-      else
-        v in
-    markup_escape v
-  with
-  | CharEncoding.Malformed_code
-  | CharEncoding.Out_of_range ->
-      (* Fallback to displaying the string as hex. *)
-      markup_hex_data v
+  try markup_escape (windows_string_to_utf8 v)
+  with CharEncoding.Malformed_code | CharEncoding.Out_of_range ->
+    (* Fallback to displaying the string as hex. *)
+    markup_hex_data v
 
 and normal (r, g, b) =
   let r = if r < 0 then 0 else if r > 255 then 255 else r in