summaryrefslogtreecommitdiff
path: root/converters.cc
blob: 02d5a268613dbcb5f3978cdf740ec7a724c01949 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Copyright (C) 2003 Mooffie <mooffie@typo.co.il>
//
// This program 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 of the License, or
// (at your option) any later version.
//
// This program 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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.

#include <config.h>

#include <ctype.h> // toupper
#include <stdio.h>

#include "converters.h"
#include "iso88598.h"
#include "utf8.h"
#include "dbg.h"

// guess_encoding() - guesses the encoding of a string.

const char *guess_encoding(const char *buf, int len)
{
#define IS_UTF8		 "UTF-8"
#define IS_UTF16	 "UTF-16"
#define IS_UTF32	 "UTF-32"
#define IS_NOT_UTF8	 NULL
#define UNKNOWN_TYPE	 NULL

    if (len >= 4) {
	// check for BOM (Byte-Order Mark):
	// 
	// UTF-16 big-endian          FE FF
	// UTF-16 little-endian       FF FE
	// UTF-32 big-endian          00 00 FE FF
	// UTF-32 little-endian       FF FE 00 00

	if (buf[0] == (char)0xFE && buf[1] == (char)0xFF)
	    return IS_UTF16; // BE
	if (buf[0] == (char)0xFF && buf[1] == (char)0xFE) {
	    if (buf[2] == 0 && buf[3] == 0)
		return IS_UTF32; // LE
	    else
		return IS_UTF16; // LE
	}
	if (buf[0] == 0 && buf[1] == 0
		&& buf[2] == (char)0xFE && buf[3] == (char)0xFF)
	    return IS_UTF32; // BE
    }

    // No BOM was found. Go through the string and check for
    // UTF-8 sequences: if an illegal sequence was found, it's
    // IS_NOT_UTF8, if all squences are legal, it's IS_UTF8, if
    // it doesn't have characters with high bit set, return
    // UNKNOWN. 

    const char *result = UNKNOWN_TYPE;
    int nbytes = 0;
    for (int i = 0; i < len; i++) {
	if (nbytes) {
	    if ((buf[i] & 0xC0) != 0x80) {
		return IS_NOT_UTF8;
	    }
	    if (! --nbytes)
		result = IS_UTF8;
	} else if (buf[i] & 0x80) {
	    const char &c = buf[i];
	    nbytes = (c & 0xE0) == 0xC0 ? 1 :
		     (c & 0xF0) == 0xE0 ? 2 :
		     (c & 0xF8) == 0xF0 ? 3 :
		     (c & 0xFC) == 0xF8 ? 4 :
		     (c & 0xFE) == 0xFC ? 5 : 0;
	    if (nbytes == 0) {
		return IS_NOT_UTF8;
	    }
	}
    }
    return result;
}

#ifdef USE_ICONV
IconvConverter::IconvConverter()
{
    cd = (iconv_t)-1;
}

IconvConverter::~IconvConverter()
{
    if (cd != (iconv_t)-1)
	iconv_close(cd);
}

int IconvConverter::set_source_encoding(const char *encoding)
{
    cd = iconv_open(INTERNAL_ENCODING, encoding);
    if (cd == (iconv_t)-1) {
	return false;
    }
    return true;
}

int IconvConverter::set_target_encoding(const char *encoding)
{
    cd = iconv_open(encoding, INTERNAL_ENCODING);
    if (cd == (iconv_t)-1) {
	return false;
    }
    return true;
}

int IconvConverter::convert(unichar **dest, char **src, int len)
{
    size_t dest_avail = 99999; // :FIXME:  I've tried several xxx_MAX constants
			       //          but it makes iconv() fail.
    size_t src_bytes_left = len;
    size_t result = iconv(cd, (ICONV_CONST char **)src, &src_bytes_left,
			  (char **)dest, &dest_avail);
    return (result == (size_t)-1) ? (int)-1 : (int)result;
}

int IconvConverter::convert(char **dest, unichar **src, int len)
{
    size_t dest_avail = 99999; // :FIXME:
    size_t src_bytes_left = len * sizeof(unichar);
    size_t result;

    while (1) {
	result = iconv(cd, (ICONV_CONST char **)src, &src_bytes_left,
			  dest, &dest_avail);
	if (ilseq_repr && result == (size_t)-1 && errno == EILSEQ) {
	    // We're asked to represent EILSEQ as "?".
	    // we put "?" in **dest, and advance the
	    // src pointer.
	    (*src)++;
	    src_bytes_left -= sizeof(unichar);
	    (**dest) = '?';
	    (*dest)++;
	    dest_avail--;
	} else {
	    break;
	}
    }
    return (result == (size_t)-1) ? (int)-1 : (int)result;
}
#endif

int ISO88598Converter::convert(unichar **dest, char **src, int len)
{
    int count = 0;
    unichar * &d = *dest;
    char *    &s = *src;
    while (len--) {
	*d++ = iso88598_to_unicode(*s++);
	count++;
    }
    return count;
}

int ISO88598Converter::convert(char **dest, unichar **src, int len)
{
    int count = 0;
    char *    &d = *dest;
    unichar * &s = *src;
    while (len--) {
	int ich = unicode_to_iso88598(*s);
	if (ich == EOF) {
	    if (ilseq_repr) {
		ich = '?';
	    } else {
		errno = EILSEQ;
		return -1;
	    }
	}
	*d++ = (char)ich;
	s++;
	count++;
    }
    return count;
}

int Latin1Converter::convert(unichar **dest, char **src, int len)
{
    int count = len;
    unichar * &d = *dest;
    char *    &s = *src;
    while (len--)
	*d++ = (unsigned char)*s++;
    return count;
}

int Latin1Converter::convert(char **dest, unichar **src, int len)
{
    int count = len;
    char *    &d = *dest;
    unichar * &s = *src;
    while (len--) {
	if (*s > 0xFF) {
	    if (ilseq_repr) {
		*d++ = '?';
		s++;		
	    } else {
	    	errno = EILSEQ;
	    	return -1;
	    }
	} else {
	    *d++ = (char)*s++;
	}
    }
    return count;
}

int UTF8Converter::convert(unichar **dest, char **src, int len)
{
    int count = 0;
    unichar * &d = *dest;
    char *    &s = *src;
    const char *problem;
    count = utf8_to_unicode(d, s, len, &problem);
    if (problem) {
	d += count;
	s = (char *)problem;
	errno = EINVAL;
	return -1;
    } else {
	d += count;
	s += len;
    }
    return count;
}

int UTF8Converter::convert(char **dest, unichar **src, int len)
{
    char *    &d = *dest;
    unichar * &s = *src;
    int nbytes = unicode_to_utf8(d, s, len);
    d += nbytes;
    s += len;
    return len;
}

Converter *ConverterFactory::get_internal_converter(const char *enc)
{
    // canonize the encoding name: remove '-', and upperace.
    u8string encoding = u8string(enc).erase_char('-').toupper_ascii();

    DBG(1, ("looking for internal '%s' converter\n", encoding.c_str()));

    if (encoding == "UTF8")
	return new UTF8Converter();
    if (encoding == "ISO88598" || encoding == "88598")
	return new ISO88598Converter();
    if (encoding == "ISO88591" || encoding == "LATIN1"
	    || encoding == "88591" || encoding == "ASCII"
	    || encoding == "USASCII")
	return new Latin1Converter();

    return NULL;
}

Converter *ConverterFactory::get_converter_from(const char *encoding)
{
#ifdef USE_ICONV
    IconvConverter *iconv = new IconvConverter();
    if (!iconv->set_source_encoding(encoding)) {
	delete iconv;
	return NULL;
    } else {
	return iconv;
    }
#else
    return ConverterFactory::get_internal_converter(encoding);
#endif
}

Converter *ConverterFactory::get_converter_to(const char *encoding)
{
#ifdef USE_ICONV
    IconvConverter *iconv = new IconvConverter();
    if (!iconv->set_target_encoding(encoding)) {
	delete iconv;
	return NULL;
    } else {
	return iconv;
    }
#else
    return ConverterFactory::get_internal_converter(encoding);
#endif
}