[Libguestfs] [PATCH 2/3] lib: Add function that encodes UTF-8 to "packed" strings or UTF-16LE as Windows does

Hilko Bengen bengen at hilluzination.de
Sun Nov 24 22:25:52 UTC 2013


---
 lib/hivex-internal.h |  1 +
 lib/utf16.c          | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+)

diff --git a/lib/hivex-internal.h b/lib/hivex-internal.h
index 64fd49a..7a548c0 100644
--- a/lib/hivex-internal.h
+++ b/lib/hivex-internal.h
@@ -275,6 +275,7 @@ extern char* _hivex_recode (char *input_encoding,
   _hivex_recode ("UTF-16LE", _input, _len, "UTF-8", NULL)
 #define _hivex_windows_latin1_to_utf8(_input, _len) \
   _hivex_recode ("LATIN1", _input, _len, "UTF-8", NULL)
+extern char* _hivex_encode_string(const char *str, size_t *size, int *utf16);
 extern size_t _hivex_utf16_string_len_in_bytes_max (const char *str, size_t len);
 
 /* util.c */
diff --git a/lib/utf16.c b/lib/utf16.c
index 6b8bf9a..437613b 100644
--- a/lib/utf16.c
+++ b/lib/utf16.c
@@ -24,6 +24,7 @@
 #include <stdlib.h>
 #include <stdint.h>
 #include <iconv.h>
+#include <string.h>
 
 #include "hivex.h"
 #include "hivex-internal.h"
@@ -86,6 +87,24 @@ _hivex_recode (char *input_encoding, const char *input, size_t input_len,
   return out;
 }
 
+/* Encode a given UTF-8 string to Latin1 (preferred) or UTF-16 for
+ * storing in the hive file, as needed.
+ */
+char*
+_hivex_encode_string(const char *str, size_t *size, int *utf16)
+{
+  char* outstr;
+  *utf16 = 0;
+  outstr = _hivex_recode ("UTF-8", str, strlen(str),
+                          "LATIN1", size);
+  if (outstr != NULL)
+    return outstr;
+  *utf16 = 1;
+  outstr = _hivex_recode ("UTF-8", str, strlen(str),
+                          "UTF-16LE", size);
+  return outstr;
+}
+
 /* Get the length of a UTF-16 format string.  Handle the string as
  * pairs of bytes, looking for the first \0\0 pair.  Only read up to
  * 'len' maximum bytes.
-- 
1.8.4.4




More information about the Libguestfs mailing list