diff options
Diffstat (limited to 'jnlib/utf8conv.c')
-rw-r--r-- | jnlib/utf8conv.c | 144 |
1 files changed, 34 insertions, 110 deletions
diff --git a/jnlib/utf8conv.c b/jnlib/utf8conv.c index fee4dc6..b5cf6a8 100644 --- a/jnlib/utf8conv.c +++ b/jnlib/utf8conv.c @@ -27,9 +27,7 @@ #include <langinfo.h> #endif #include <errno.h> -#ifndef HAVE_W32_SYSTEM -# include <iconv.h> -#endif +#include <iconv.h> #include "libjnlib-config.h" #include "stringhelp.h" @@ -45,59 +43,6 @@ static int no_translation; /* Set to true if we let simply pass through. */ static int use_iconv; /* iconv comversion fucntions required. */ -/* Under W32 we dlopen the iconv dll and don't require any iconv - related headers at all. However we need to define some stuff. */ -#ifdef HAVE_W32_SYSTEM -typedef void *iconv_t; -#ifndef ICONV_CONST -#define ICONV_CONST const -#endif -static iconv_t (* __stdcall iconv_open) (const char *tocode, - const char *fromcode); -static size_t (* __stdcall iconv) (iconv_t cd, - const char **inbuf, size_t *inbytesleft, - char **outbuf, size_t *outbytesleft); -static int (* __stdcall iconv_close) (iconv_t cd); - -static int -load_libiconv (void) -{ - static int done; - - if (!done) - { - void *handle; - - done = 1; /* Do it right now because we might get called recursivly - through gettext. */ - - handle = dlopen ("iconv.dll", RTLD_LAZY); - if (handle) - { - iconv_open = dlsym (handle, "libiconv_open"); - if (iconv_open) - iconv = dlsym (handle, "libiconv"); - if (iconv) - iconv_close = dlsym (handle, "libiconv_close"); - } - if (!handle || !iconv_close) - { - log_info (_("error loading `%s': %s\n"), - "iconv.dll", dlerror ()); - log_info (_("please see %s for more information\n"), - "http://www.gnupg.org/download/iconv.html"); - iconv_open = NULL; - iconv = NULL; - iconv_close = NULL; - if (handle) - dlclose (handle); - } - } - return iconv_open? 0: -1; -} -#endif /*HAVE_W32_SYSTEM*/ - - /* Error handler for iconv failures. This is needed to not clutter the output with repeated diagnostics about a missing conversion. */ static void @@ -152,13 +97,13 @@ set_native_charset (const char *newset) { const char *full_newset; - if (!newset) + if (!newset) { #ifdef HAVE_W32_SYSTEM static char codepage[30]; unsigned int cpno; const char *aliases; - + /* We are a console program thus we need to use the GetConsoleOutputCP function and not the the GetACP which would give the codepage for a GUI program. Note this is not @@ -201,7 +146,7 @@ set_native_charset (const char *newset) } #else /*!HAVE_W32_SYSTEM*/ - + #ifdef HAVE_LANGINFO_CODESET newset = nl_langinfo (CODESET); #else /*!HAVE_LANGINFO_CODESET*/ @@ -225,7 +170,7 @@ set_native_charset (const char *newset) mod = strchr (++dot, '@'); if (!mod) mod = dot + strlen (dot); - if (mod - dot < sizeof codepage && dot != mod) + if (mod - dot < sizeof codepage && dot != mod) { memcpy (codepage, dot, mod - dot); codepage [mod - dot] = 0; @@ -272,21 +217,16 @@ set_native_charset (const char *newset) else { iconv_t cd; - -#ifdef HAVE_W32_SYSTEM - if (load_libiconv ()) - return -1; -#endif /*HAVE_W32_SYSTEM*/ cd = iconv_open (full_newset, "utf-8"); - if (cd == (iconv_t)-1) + if (cd == (iconv_t)-1) { handle_iconv_error (full_newset, "utf-8", 0); return -1; } iconv_close (cd); cd = iconv_open ("utf-8", full_newset); - if (cd == (iconv_t)-1) + if (cd == (iconv_t)-1) { handle_iconv_error ("utf-8", full_newset, 0); return -1; @@ -306,7 +246,7 @@ get_native_charset () } /* Return true if the native charset is utf-8. */ -int +int is_native_utf8 (void) { return no_translation; @@ -353,13 +293,13 @@ native_to_utf8 (const char *orig_string) *p = 0; } else - { + { /* Need to use iconv. */ iconv_t cd; const char *inptr; char *outptr; size_t inbytes, outbytes; - + cd = iconv_open ("utf-8", active_charset_name); if (cd == (iconv_t)-1) { @@ -367,14 +307,14 @@ native_to_utf8 (const char *orig_string) return native_to_utf8 (string); } - for (s=string; *s; s++ ) + for (s=string; *s; s++ ) { length++; if ((*s & 0x80)) length += 5; /* We may need up to 6 bytes for the utf8 output. */ } buffer = jnlib_xmalloc (length + 1); - + inptr = string; inbytes = strlen (string); outptr = buffer; @@ -448,10 +388,10 @@ do_utf8_to_native (const char *string, size_t length, int delim, if (!nleft) { if (!(*s & 0x80)) - { + { /* Plain ascii. */ if ( delim != -1 - && (*s < 0x20 || *s == 0x7f || *s == delim + && (*s < 0x20 || *s == 0x7f || *s == delim || (delim && *s == '\\'))) { n++; @@ -490,35 +430,35 @@ do_utf8_to_native (const char *string, size_t length, int delim, encbuf[encidx++] = *s; } else if ((*s & 0xf0) == 0xe0) /* 1110 xxxx */ - { + { val = *s & 0x0f; nleft = 2; encidx = 0; encbuf[encidx++] = *s; } else if ((*s & 0xf8) == 0xf0) /* 1111 0xxx */ - { + { val = *s & 0x07; nleft = 3; encidx = 0; encbuf[encidx++] = *s; } else if ((*s & 0xfc) == 0xf8) /* 1111 10xx */ - { + { val = *s & 0x03; nleft = 4; encidx = 0; encbuf[encidx++] = *s; } else if ((*s & 0xfe) == 0xfc) /* 1111 110x */ - { + { val = *s & 0x01; nleft = 5; encidx = 0; encbuf[encidx++] = *s; } else /* Invalid encoding: print as \xNN. */ - { + { if (p) { sprintf (p, "\\x%02x", *s); @@ -551,7 +491,7 @@ do_utf8_to_native (const char *string, size_t length, int delim, val <<= 6; val |= *s & 0x3f; if (!--nleft) /* Ready. */ - { + { if (no_translation) { if (p) @@ -590,12 +530,12 @@ do_utf8_to_native (const char *string, size_t length, int delim, if (val >= 0x80 && val < 256) { /* We can simply print this character */ - n++; + n++; if (p) *p++ = val; } else - { + { /* We do not have a translation: print utf8. */ if (p) { @@ -625,7 +565,7 @@ do_utf8_to_native (const char *string, size_t length, int delim, const char *inptr; char *outbuf, *outptr; size_t inbytes, outbytes; - + *p = 0; /* Terminate the buffer. */ cd = iconv_open (active_charset_name, "utf-8"); @@ -642,14 +582,14 @@ do_utf8_to_native (const char *string, size_t length, int delim, inbytes = n - 1;; inptr = buffer; outbytes = n * MB_LEN_MAX; - if (outbytes / MB_LEN_MAX != n) + if (outbytes / MB_LEN_MAX != n) BUG (); /* Actually an overflow. */ outbuf = outptr = jnlib_xmalloc (outbytes); if ( iconv (cd, (ICONV_CONST char **)&inptr, &inbytes, - &outptr, &outbytes) == (size_t)-1) + &outptr, &outbytes) == (size_t)-1) { static int shown; - + if (!shown) log_info (_("conversion from `%s' to `%s' failed: %s\n"), "utf-8", active_charset_name, strerror (errno)); @@ -661,7 +601,7 @@ do_utf8_to_native (const char *string, size_t length, int delim, outbuf = do_utf8_to_native (string, length, delim, 0); } else /* Success. */ - { + { *outptr = 0; /* Make sure it is a string. */ /* We could realloc the buffer now but I doubt that it makes much sense given that it will get freed @@ -694,45 +634,29 @@ utf8_to_native (const char *string, size_t length, int delim) -/* Wrapper function for iconv_open, required for W32 as we dlopen that - library on that system. */ -jnlib_iconv_t +/* Wrapper function for iconv_open, formerly required for W32 as we + used to dlopen that library on that system. */ +jnlib_iconv_t jnlib_iconv_open (const char *tocode, const char *fromcode) { -#ifdef HAVE_W32_SYSTEM - if (load_libiconv ()) - return (jnlib_iconv_t)(-1); -#endif /*HAVE_W32_SYSTEM*/ - return (jnlib_iconv_t)iconv_open (tocode, fromcode); } -/* Wrapper function for iconv, required for W32 as we dlopen that - library on that system. */ +/* Wrapper function for iconv, formerly required for W32 as we used to + dlopen that library on that system. */ size_t jnlib_iconv (jnlib_iconv_t cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft) { - -#ifdef HAVE_W32_SYSTEM - if (load_libiconv ()) - return 0; -#endif /*HAVE_W32_SYSTEM*/ - return iconv ((iconv_t)cd, (char**)inbuf, inbytesleft, outbuf, outbytesleft); } -/* Wrapper function for iconv_close, required for W32 as we dlopen that - library on that system. */ +/* Wrapper function for iconv_close, formerly required for W32 as we + used to dlopen that library on that system. */ int jnlib_iconv_close (jnlib_iconv_t cd) { -#ifdef HAVE_W32_SYSTEM - if (load_libiconv ()) - return 0; -#endif /*HAVE_W32_SYSTEM*/ - return iconv_close ((iconv_t)cd); } |