~siretart/lcd4linux/debian

« back to all changes in this revision

Viewing changes to plugin_iconv.c

  • Committer: Reinhard Tartler
  • Date: 2011-04-27 17:24:15 UTC
  • mto: This revision was merged to the branch mainline in revision 750.
  • Revision ID: siretart@tauware.de-20110427172415-6n4aptmvmz0eztvm
Tags: upstream-0.11.0~svn1143
ImportĀ upstreamĀ versionĀ 0.11.0~svn1143

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 $
 
3
 *
 
4
 * iconv charset conversion plugin
 
5
 *
 
6
 * Copyright (C) 2006 Ernst Bachmann <e.bachmann@xebec.de>
 
7
 * Copyright (C) 2006 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
 
8
 *
 
9
 * This file is part of LCD4Linux.
 
10
 *
 
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)
 
14
 * any later version.
 
15
 *
 
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.
 
20
 *
 
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.
 
24
 *
 
25
 */
 
26
 
 
27
/* 
 
28
 * exported functions:
 
29
 *
 
30
 * int plugin_init_iconv (void)
 
31
 * int plugin_exit_iconv (void)
 
32
 *
 
33
 */
 
34
 
 
35
 
 
36
#include "config.h"
 
37
 
 
38
#include <stdlib.h>
 
39
#include <string.h>
 
40
#include <ctype.h>
 
41
#include <iconv.h>
 
42
#include <errno.h>
 
43
 
 
44
/* these should always be included */
 
45
#include "debug.h"
 
46
#include "plugin.h"
 
47
 
 
48
#ifdef WITH_DMALLOC
 
49
#include <dmalloc.h>
 
50
#endif
 
51
 
 
52
 
 
53
 
 
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)
 
58
{
 
59
    char *source;
 
60
    size_t source_left;
 
61
    char *dest;
 
62
    char *dest_pos;
 
63
    size_t dest_left;
 
64
    iconv_t cd;
 
65
 
 
66
    source = R2S(arg);
 
67
    source_left = strlen(source);
 
68
 
 
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. */
 
72
 
 
73
    dest_left = 2 * source_left;
 
74
    dest = malloc(dest_left + 1);
 
75
    dest_pos = dest;
 
76
 
 
77
    cd = iconv_open(R2S(charset_to), R2S(charset_from));
 
78
    if (cd != (iconv_t) (-1)) {
 
79
 
 
80
        do {
 
81
 
 
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)) {
 
84
                switch (errno) {
 
85
                case EILSEQ:
 
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);
 
89
                    source_left--;
 
90
                    source++;
 
91
                    break;
 
92
                case EINVAL:
 
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");
 
96
                    source_left = 0;
 
97
                    break;
 
98
                case E2BIG:
 
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 */
 
102
                    error
 
103
                        ("plugin_iconv: out of memory in destination buffer. Seems like Ernst was too lazy, complain to him!");
 
104
                    source_left = 0;
 
105
                    break;
 
106
                default:
 
107
                    error("plugin_iconv: strange errno state (%d) occurred", errno);
 
108
                    source_left = 0;
 
109
                }
 
110
            }
 
111
        } while (source_left > 0);      /* don't check for == 0, could be negative in EILSEQ case */
 
112
 
 
113
        /* terminate the string, we're sure to have that byte left, see above */
 
114
        *dest_pos = 0;
 
115
        dest_pos++;
 
116
 
 
117
        iconv_close(cd);
 
118
    } else {
 
119
        error("plugin_iconv: could not open conversion descriptor. Check if your charsets are supported!");
 
120
        /* guaranteed to fit. */
 
121
        strcpy(dest, source);
 
122
    }
 
123
 
 
124
    SetResult(&result, R_STRING, dest);
 
125
 
 
126
    free(dest);
 
127
}
 
128
 
 
129
 
 
130
/* plugin initialization */
 
131
int plugin_init_iconv(void)
 
132
{
 
133
 
 
134
    AddFunction("iconv", 3, my_iconv);
 
135
 
 
136
    return 0;
 
137
}
 
138
 
 
139
void plugin_exit_iconv(void)
 
140
{
 
141
    /* nothing to clean */
 
142
}