[Libguestfs] [PATCH 3/3] Correctly handle latin1-encoded key/value names

Hilko Bengen
Fri, 22 Nov 2013 13:08:35 -0000

---
 lib/hivex-internal.h | 6 +++++-
 lib/node.c           | 7 +------
 lib/utf16.c          | 5 ++---
 lib/value.c          | 7 +------
 4 files changed, 9 insertions(+), 16 deletions(-)

diff --git a/lib/hivex-internal.h b/lib/hivex-internal.h
index d7ce339..4135f58 100644
--- a/lib/hivex-internal.h
+++ b/lib/hivex-internal.h
@@ -268,7 +268,11 @@ extern size_t * _hivex_return_offset_list (offset_list
*list);
 extern void _hivex_print_offset_list (offset_list *list, FILE *fp);

 /* utf16.c */
-extern char *_hivex_windows_utf16_to_utf8 (/* const */ char *input, size_t
len);
+extern char* _hivex_to_utf8 (/* const */ char *input, size_t len, char*
input_encoding);
+#define _hivex_windows_utf16_to_utf8(_input, _len) \
+  _hivex_to_utf8 (_input, _len, "UTF-16LE")
+#define _hivex_windows_latin1_to_utf8(_input, _len) \
+  _hivex_to_utf8 (_input, _len, "LATIN1")
 extern size_t _hivex_utf16_string_len_in_bytes_max (const char *str, size_t
len);

 /* util.c */
diff --git a/lib/node.c b/lib/node.c
index fda4592..fcd7442 100644
--- a/lib/node.c
+++ b/lib/node.c
@@ -90,12 +90,7 @@ hivex_node_name (hive_h *h, hive_node_h node)
   }
   size_t flags = le16toh (nk->flags);
   if (flags & 0x20) {
-    char *ret = malloc (len + 1);
-    if (ret == NULL)
-      return NULL;
-    memcpy (ret, nk->name, len);
-    ret[len] = '\0';
-    return ret;
+    return _hivex_windows_latin1_to_utf8 (nk->name, len);
   } else {
     return _hivex_windows_utf16_to_utf8 (nk->name, len);
   }
diff --git a/lib/utf16.c b/lib/utf16.c
index 844715c..eca2343 100644
--- a/lib/utf16.c
+++ b/lib/utf16.c
@@ -29,15 +29,14 @@
 #include "hivex-internal.h"

 char *
-_hivex_windows_utf16_to_utf8 (/* const */ char *input, size_t len)
+_hivex_to_utf8 (/* const */ char *input, size_t len, char* input_encoding)
 {
-  iconv_t ic = iconv_open ("UTF-8", "UTF-16LE");
+  iconv_t ic = iconv_open ("UTF-8", input_encoding);
   if (ic == (iconv_t) -1)
     return NULL;

   /* iconv(3) has an insane interface ... */

-  /* Mostly UTF-8 will be smaller, so this is a good initial guess. */
   size_t outalloc = len;

  again:;
diff --git a/lib/value.c b/lib/value.c
index 66cde48..3460a8c 100644
--- a/lib/value.c
+++ b/lib/value.c
@@ -215,12 +215,7 @@ hivex_value_key (hive_h *h, hive_value_h value)
     return NULL;
   size_t flags = le16toh (vk->flags);
   if (flags & 0x01) {
-    char *ret = malloc (len + 1);
-    if (ret == NULL)
-      return NULL;
-    memcpy (ret, vk->name, len);
-    ret[len] = '\0';
-    return ret;
+    return _hivex_windows_latin1_to_utf8 (vk->name, len);
   } else {
     return _hivex_windows_utf16_to_utf8 (vk->name, len);
   }
--
1.8.4.3