Extra arguments for the length of non-UTF-8 strings.
authorPeter Verthez <Peter.Verthez@advalvas.be>
Sun, 1 Dec 2002 15:25:20 +0000 (15:25 +0000)
committerPeter Verthez <Peter.Verthez@advalvas.be>
Sun, 1 Dec 2002 15:25:20 +0000 (15:25 +0000)
utf8/utf8-convert.c
utf8/utf8-locale.c
utf8/utf8.h

index 097f199a35a6c4448d03dfe7892899f4ad13bcb3..926e14c41305d7e4741caf3fc3eb27331c06544b 100644 (file)
@@ -187,7 +187,8 @@ void cleanup_utf8_conversion(convert_t conv)
   }
 }
 
-char* convert_from_utf8(convert_t conv, const char* input, int* conv_fails)
+char* convert_from_utf8(convert_t conv, const char* input, int* conv_fails,
+                       size_t* output_len)
 {
   size_t insize = strlen(input);
   size_t outsize;
@@ -246,12 +247,12 @@ char* convert_from_utf8(convert_t conv, const char* input, int* conv_fails)
     }
     nconv = iconv(conv->from_utf8, &inptr, &insize, &outptr, &outsize);
   }
+  if (output_len) *output_len = outptr - outbuf->buffer;
   return outbuf->buffer;
 }
 
-char* convert_to_utf8(convert_t conv, const char* input)
+char* convert_to_utf8(convert_t conv, const char* input, size_t input_len)
 {
-  size_t insize  = strlen(input);
   size_t outsize;
   ICONV_CONST char *inptr  = (ICONV_CONST char*) input;
   char   *outptr;
@@ -267,7 +268,7 @@ char* convert_to_utf8(convert_t conv, const char* input)
   outptr  = outbuf->buffer;
   outsize = outbuf->size;
   reset_conv_buffer(conv->outbuf);
-  nconv = iconv(conv->to_utf8, &inptr, &insize, &outptr, &outsize);
+  nconv = iconv(conv->to_utf8, &inptr, &input_len, &outptr, &outsize);
   while (nconv == (size_t)-1) {
     if (errno == E2BIG) {
       /* grow the output buffer */
@@ -286,7 +287,7 @@ char* convert_to_utf8(convert_t conv, const char* input)
       /* EBADF is an error which should be captured by the first if above */
       return NULL;
     }
-    nconv = iconv(conv->to_utf8, &inptr, &insize, &outptr, &outsize);
+    nconv = iconv(conv->to_utf8, &inptr, &input_len, &outptr, &outsize);
   }
   return outbuf->buffer;  
 }
index 9ede577d17cc6c78caf46cbfad4210e04522fec6..a59b8ce5fe98b685bfcc92d1fbc813d2c9f84e6a 100644 (file)
@@ -12,6 +12,7 @@
 #include "utf8.h"
 #include <stdlib.h>
 #include <assert.h>
+#include <string.h>
 #include "libcharset.h"
 
 static convert_t locale_conv = NULL;
@@ -47,7 +48,7 @@ char* convert_utf8_to_locale(const char* input, int *conv_fails)
   if (!locale_conv)
     open_conversion_contexts();
 
-  return convert_from_utf8(locale_conv, input, conv_fails);
+  return convert_from_utf8(locale_conv, input, conv_fails, NULL);
 }
 
 char* convert_locale_to_utf8(const char* input)
@@ -55,5 +56,5 @@ char* convert_locale_to_utf8(const char* input)
   if (!locale_conv)
     open_conversion_contexts();
 
-  return convert_to_utf8(locale_conv, input);
+  return convert_to_utf8(locale_conv, input, strlen(input));
 }
index 131dfbc3d245b6706ec23d207ec752a2fdc6b675..3cfb1c13be35ba51a41679bdabc86b78c878eae4 100644 (file)
@@ -41,8 +41,9 @@ convert_t initialize_utf8_conversion(const char* charset, int external_outbuf);
 int   conversion_set_unknown(convert_t conv, const char* unknown);
 int   conversion_set_output_buffer(convert_t conv, conv_buffer_t buf);
 void  cleanup_utf8_conversion(convert_t conv);
-char* convert_from_utf8(convert_t conv, const char* input, int* conv_fails);
-char* convert_to_utf8(convert_t conv, const char* input);
+char* convert_from_utf8(convert_t conv, const char* input, int* conv_fails,
+                       size_t* output_len);
+char* convert_to_utf8(convert_t conv, const char* input, size_t input_len);
 char* convert_to_utf8_incremental(convert_t conv,
                                  const char* input, size_t input_len);