[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[Libguestfs] [PATCH 1/3] lib: Further generalize iconv wrapper function.



---
 lib/hivex-internal.h |  8 +++++---
 lib/utf16.c          | 11 +++++++----
 2 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/lib/hivex-internal.h b/lib/hivex-internal.h
index 4135f58..64fd49a 100644
--- a/lib/hivex-internal.h
+++ b/lib/hivex-internal.h
@@ -268,11 +268,13 @@ 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_to_utf8 (/* const */ char *input, size_t len, char* input_encoding);
+extern char* _hivex_recode (char *input_encoding,
+                            const char *input, size_t input_len,
+                            char *output_encoding, size_t *output_len);
 #define _hivex_windows_utf16_to_utf8(_input, _len) \
-  _hivex_to_utf8 (_input, _len, "UTF-16LE")
+  _hivex_recode ("UTF-16LE", _input, _len, "UTF-8", NULL)
 #define _hivex_windows_latin1_to_utf8(_input, _len) \
-  _hivex_to_utf8 (_input, _len, "LATIN1")
+  _hivex_recode ("LATIN1", _input, _len, "UTF-8", NULL)
 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 eca2343..6b8bf9a 100644
--- a/lib/utf16.c
+++ b/lib/utf16.c
@@ -29,18 +29,19 @@
 #include "hivex-internal.h"
 
 char *
-_hivex_to_utf8 (/* const */ char *input, size_t len, char* input_encoding)
+_hivex_recode (char *input_encoding, const char *input, size_t input_len,
+               char *output_encoding, size_t *output_len)
 {
-  iconv_t ic = iconv_open ("UTF-8", input_encoding);
+  iconv_t ic = iconv_open (output_encoding, input_encoding);
   if (ic == (iconv_t) -1)
     return NULL;
 
   /* iconv(3) has an insane interface ... */
 
-  size_t outalloc = len;
+  size_t outalloc = input_len;
 
  again:;
-  size_t inlen = len;
+  size_t inlen = input_len;
   size_t outlen = outalloc;
   char *out = malloc (outlen + 1);
   if (out == NULL) {
@@ -79,6 +80,8 @@ _hivex_to_utf8 (/* const */ char *input, size_t len, char* input_encoding)
 
   *outp = '\0';
   iconv_close (ic);
+  if (output_len != NULL)
+    *output_len = outp - out;
 
   return out;
 }
-- 
1.8.4.4


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]