1
/* $Id: plugin_iconv.c 1136 2010-11-28 16:07:16Z mzuther $
2
* $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/plugin_iconv.c $
4
* iconv charset conversion plugin
6
* Copyright (C) 2006 Ernst Bachmann <e.bachmann@xebec.de>
7
* Copyright (C) 2006 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
9
* This file is part of LCD4Linux.
11
* LCD4Linux is free software; you can redistribute it and/or modify
12
* it under the terms of the GNU General Public License as published by
13
* the Free Software Foundation; either version 2, or (at your option)
16
* LCD4Linux is distributed in the hope that it will be useful,
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
* GNU General Public License for more details.
21
* You should have received a copy of the GNU General Public License
22
* along with this program; if not, write to the Free Software
23
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30
* int plugin_init_iconv (void)
31
* int plugin_exit_iconv (void)
44
/* these should always be included */
54
/* iconv function, convert charsets */
55
/* valid "to" and "from" charsets can be listed by running "iconv --list" from a shell */
56
/* utf16 & utf32 encodings won't work, as they contain null bytes, confusing strlen */
57
static void my_iconv(RESULT * result, RESULT * charset_from, RESULT * charset_to, RESULT * arg)
67
source_left = strlen(source);
69
/* use twice the memory needed in best case, but save lots of reallocs in worst case */
70
/* increase to 4 if most conversions are to utf32 (quite unlikely) */
71
/* also alloc a "safety byte" so we can always zero-terminate the string. */
73
dest_left = 2 * source_left;
74
dest = malloc(dest_left + 1);
77
cd = iconv_open(R2S(charset_to), R2S(charset_from));
78
if (cd != (iconv_t) (-1)) {
82
/* quite spammy: debug("plugin_iconv: calling iconv with %ld,[%s]/%ld,%ld", cd, source, source_left, dest_left); */
83
if (iconv(cd, &source, &source_left, &dest_pos, &dest_left) == (size_t) (-1)) {
86
/* illegal bytes in input sequence */
87
/* try to fix by skipping a byte */
88
info("plugin_iconv: illegal character in input string: %c", *source);
93
/* input string ends during a multibyte sequence */
94
/* try to fix by simply ignoring */
95
info("plugin_iconv: illegal character at end of input");
99
/* not enough bytes in outbuf. */
100
/* TODO: Realloc output buffer, probably doubling its size? */
101
/* for now, just bail out. For lcd4linux 99% of all conversions will go to ascii or latin1 anyways */
103
("plugin_iconv: out of memory in destination buffer. Seems like Ernst was too lazy, complain to him!");
107
error("plugin_iconv: strange errno state (%d) occurred", errno);
111
} while (source_left > 0); /* don't check for == 0, could be negative in EILSEQ case */
113
/* terminate the string, we're sure to have that byte left, see above */
119
error("plugin_iconv: could not open conversion descriptor. Check if your charsets are supported!");
120
/* guaranteed to fit. */
121
strcpy(dest, source);
124
SetResult(&result, R_STRING, dest);
130
/* plugin initialization */
131
int plugin_init_iconv(void)
134
AddFunction("iconv", 3, my_iconv);
139
void plugin_exit_iconv(void)
141
/* nothing to clean */