/* $Id$ * $URL$ * * iconv charset conversion plugin * * Copyright (C) 2006 Ernst Bachmann * Copyright (C) 2006 The LCD4Linux Team * * This file is part of LCD4Linux. * * LCD4Linux is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * LCD4Linux is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * */ /* * exported functions: * * int plugin_init_iconv (void) * int plugin_exit_iconv (void) * */ #include "config.h" #include #include #include #include #include /* these should always be included */ #include "debug.h" #include "plugin.h" #ifdef WITH_DMALLOC #include #endif /* iconv function, convert charsets */ /* valid "to" and "from" charsets can be listed by running "iconv --list" from a shell */ /* utf16 & utf32 encodings won't work, as they contain null bytes, confusing strlen */ static void my_iconv(RESULT * result, RESULT * charset_from, RESULT * charset_to, RESULT * arg) { char *source; size_t source_left; char *dest; char *dest_pos; size_t dest_left; iconv_t cd; source = R2S(arg); source_left = strlen(source); /* use twice the memory needed in best case, but save lots of reallocs in worst case */ /* increase to 4 if most conversions are to utf32 (quite unlikely) */ /* also alloc a "safety byte" so we can always zero-terminate the string. */ dest_left = 2 * source_left; dest = malloc(dest_left + 1); dest_pos = dest; cd = iconv_open(R2S(charset_to), R2S(charset_from)); if (cd != (iconv_t) (-1)) { do { /* quite spammy: debug("plugin_iconv: calling iconv with %ld,[%s]/%ld,%ld", cd, source, source_left, dest_left); */ if (iconv(cd, &source, &source_left, &dest_pos, &dest_left) == (size_t) (-1)) { switch (errno) { case EILSEQ: /* illegal bytes in input sequence */ /* try to fix by skipping a byte */ info("plugin_iconv: illegal character in input string: %c", *source); source_left--; source++; break; case EINVAL: /* input string ends during a multibyte sequence */ /* try to fix by simply ignoring */ info("plugin_iconv: illegal character at end of input"); source_left = 0; break; case E2BIG: /* not enough bytes in outbuf. */ /* TODO: Realloc output buffer, probably doubling its size? */ /* for now, just bail out. For lcd4linux 99% of all conversions will go to ascii or latin1 anyways */ error ("plugin_iconv: out of memory in destination buffer. Seems like Ernst was too lazy, complain to him!"); source_left = 0; break; default: error("plugin_iconv: strange errno state (%d) occured", errno); source_left = 0; } } } while (source_left > 0); /* don't check for == 0, could be negative in EILSEQ case */ /* terminate the string, we're sure to have that byte left, see above */ *dest_pos = 0; dest_pos++; iconv_close(cd); } else { error("plugin_iconv: could not open conversion descriptor. Check if your charsets are supported!"); /* guaranteed to fit. */ strcpy(dest, source); } SetResult(&result, R_STRING, dest); free(dest); } /* plugin initialization */ int plugin_init_iconv(void) { AddFunction("iconv", 3, my_iconv); return 0; } void plugin_exit_iconv(void) { /* nothing to clean */ } 6a85893c026&follow=1'>[lcd4linux @ 2004-06-26 12:04:59 by reinelt]reinelt69-139/+413 2004-06-26[lcd4linux @ 2004-06-26 09:27:20 by reinelt]reinelt71-1842/+2274 2004-06-26[lcd4linux @ 2004-06-26 06:12:14 by reinelt]reinelt7-111/+562 2004-06-24[lcd4linux @ 2004-06-24 20:18:08 by nicowallmeier]nicowallmeier1-2/+5 2004-06-20[lcd4linux @ 2004-06-20 10:12:27 by reinelt]reinelt1-0/+110 2004-06-20[lcd4linux @ 2004-06-20 10:09:52 by reinelt]reinelt51-377/+569 2004-06-19[lcd4linux @ 2004-06-19 08:20:19 by reinelt]reinelt2-7/+17 2004-06-17[lcd4linux @ 2004-06-17 10:58:57 by reinelt]reinelt3-76/+84 2004-06-17[lcd4linux @ 2004-06-17 06:23:39 by reinelt]reinelt19-403/+633 2004-06-13[lcd4linux @ 2004-06-13 01:12:52 by reinelt]reinelt3-18/+34 2004-06-09[lcd4linux @ 2004-06-09 06:40:29 by reinelt]reinelt3-11/+35 2004-06-08[lcd4linux @ 2004-06-08 21:46:38 by reinelt]reinelt5-31/+131 2004-06-08[lcd4linux @ 2004-06-08 12:35:24 by reinelt]reinelt6-882/+474 2004-06-07[lcd4linux @ 2004-06-07 07:02:13 by reinelt]reinelt1-0/+33 2004-06-07[lcd4linux @ 2004-06-07 06:56:55 by reinelt]reinelt5-7/+149 2004-06-06[lcd4linux @ 2004-06-06 06:51:59 by reinelt]reinelt16-52/+135 2004-06-05[lcd4linux @ 2004-06-05 14:56:48 by reinelt]reinelt3-24/+37 2004-06-05[lcd4linux @ 2004-06-05 06:41:39 by reinelt]reinelt11-42/+87 2004-06-05[lcd4linux @ 2004-06-05 06:13:11 by reinelt]reinelt9-69/+327 2004-06-02[lcd4linux @ 2004-06-02 10:09:22 by reinelt]reinelt4-180/+183 2004-06-02[lcd4linux @ 2004-06-02 09:41:19 by reinelt]reinelt18-114/+211 2004-06-02[lcd4linux @ 2004-06-02 05:56:25 by reinelt]reinelt1-2/+6 2004-06-02[lcd4linux @ 2004-06-02 05:35:55 by reinelt]reinelt1-0/+20 2004-06-02[lcd4linux @ 2004-06-02 05:27:59 by reinelt]reinelt26-0/+2405 2004-06-02[lcd4linux @ 2004-06-02 05:14:16 by reinelt]reinelt2-17/+31 2004-06-01[lcd4linux @ 2004-06-01 06:45:28 by reinelt]reinelt18-71/+241 2004-06-01[lcd4linux @ 2004-06-01 06:04:25 by reinelt]reinelt2-25/+25 2004-05-31[lcd4linux @ 2004-05-31 21:23:16 by reinelt]reinelt1-37/+49 2004-05-31[lcd4linux @ 2004-05-31 21:05:13 by reinelt]reinelt5-77/+111 2004-05-31[lcd4linux @ 2004-05-31 16:39:05 by reinelt]reinelt11-180/+535 2004-05-31[lcd4linux @ 2004-05-31 06:27:34 by reinelt]reinelt1-0/+40 2004-05-31[lcd4linux @ 2004-05-31 06:24:42 by reinelt]reinelt2-7/+29 2004-05-31[lcd4linux @ 2004-05-31 05:38:02 by reinelt]reinelt8-42/+94 2004-05-31[lcd4linux @ 2004-05-31 01:31:01 by andy-b]andy-b1-10/+12 2004-05-30[lcd4linux @ 2004-05-30 08:25:50 by reinelt]reinelt2-30/+92 2004-05-29[lcd4linux @ 2004-05-29 23:30:20 by reinelt]reinelt1-1/+9 2004-05-29[lcd4linux @ 2004-05-29 15:53:28 by reinelt]reinelt3-9/+25 2004-05-29[lcd4linux @ 2004-05-29 01:07:56 by reinelt]reinelt2-21/+25 2004-05-29[lcd4linux @ 2004-05-29 00:27:14 by reinelt]reinelt8-11/+214 2004-05-28[lcd4linux @ 2004-05-28 14:38:10 by reinelt]reinelt2-1/+41