1
/* utf8conf.c - UTF8 character set conversion
2
* Copyright (C) 1994, 1998, 1999, 2000, 2001,
3
* 2003 Free Software Foundation, Inc.
5
* This file is part of GnuPG.
7
* GnuPG is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* GnuPG is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
27
#ifdef HAVE_LANGINFO_CODESET
31
#include "libjnlib-config.h"
32
#include "stringhelp.h"
36
static ushort koi8_unicode[128] = {
37
0x2500, 0x2502, 0x250c, 0x2510, 0x2514, 0x2518, 0x251c, 0x2524,
38
0x252c, 0x2534, 0x253c, 0x2580, 0x2584, 0x2588, 0x258c, 0x2590,
39
0x2591, 0x2592, 0x2593, 0x2320, 0x25a0, 0x2219, 0x221a, 0x2248,
40
0x2264, 0x2265, 0x00a0, 0x2321, 0x00b0, 0x00b2, 0x00b7, 0x00f7,
41
0x2550, 0x2551, 0x2552, 0x0451, 0x2553, 0x2554, 0x2555, 0x2556,
42
0x2557, 0x2558, 0x2559, 0x255a, 0x255b, 0x255c, 0x255d, 0x255e,
43
0x255f, 0x2560, 0x2561, 0x0401, 0x2562, 0x2563, 0x2564, 0x2565,
44
0x2566, 0x2567, 0x2568, 0x2569, 0x256a, 0x256b, 0x256c, 0x00a9,
45
0x044e, 0x0430, 0x0431, 0x0446, 0x0434, 0x0435, 0x0444, 0x0433,
46
0x0445, 0x0438, 0x0439, 0x043a, 0x043b, 0x043c, 0x043d, 0x043e,
47
0x043f, 0x044f, 0x0440, 0x0441, 0x0442, 0x0443, 0x0436, 0x0432,
48
0x044c, 0x044b, 0x0437, 0x0448, 0x044d, 0x0449, 0x0447, 0x044a,
49
0x042e, 0x0410, 0x0411, 0x0426, 0x0414, 0x0415, 0x0424, 0x0413,
50
0x0425, 0x0418, 0x0419, 0x041a, 0x041b, 0x041c, 0x041d, 0x041e,
51
0x041f, 0x042f, 0x0420, 0x0421, 0x0422, 0x0423, 0x0416, 0x0412,
52
0x042c, 0x042b, 0x0417, 0x0428, 0x042d, 0x0429, 0x0427, 0x042a
55
static ushort latin2_unicode[128] = {
56
0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
57
0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
58
0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
59
0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F,
60
0x00A0, 0x0104, 0x02D8, 0x0141, 0x00A4, 0x013D, 0x015A, 0x00A7,
61
0x00A8, 0x0160, 0x015E, 0x0164, 0x0179, 0x00AD, 0x017D, 0x017B,
62
0x00B0, 0x0105, 0x02DB, 0x0142, 0x00B4, 0x013E, 0x015B, 0x02C7,
63
0x00B8, 0x0161, 0x015F, 0x0165, 0x017A, 0x02DD, 0x017E, 0x017C,
64
0x0154, 0x00C1, 0x00C2, 0x0102, 0x00C4, 0x0139, 0x0106, 0x00C7,
65
0x010C, 0x00C9, 0x0118, 0x00CB, 0x011A, 0x00CD, 0x00CE, 0x010E,
66
0x0110, 0x0143, 0x0147, 0x00D3, 0x00D4, 0x0150, 0x00D6, 0x00D7,
67
0x0158, 0x016E, 0x00DA, 0x0170, 0x00DC, 0x00DD, 0x0162, 0x00DF,
68
0x0155, 0x00E1, 0x00E2, 0x0103, 0x00E4, 0x013A, 0x0107, 0x00E7,
69
0x010D, 0x00E9, 0x0119, 0x00EB, 0x011B, 0x00ED, 0x00EE, 0x010F,
70
0x0111, 0x0144, 0x0148, 0x00F3, 0x00F4, 0x0151, 0x00F6, 0x00F7,
71
0x0159, 0x016F, 0x00FA, 0x0171, 0x00FC, 0x00FD, 0x0163, 0x02D9
75
static const char *active_charset_name = "iso-8859-1";
76
static ushort *active_charset = NULL;
77
static int no_translation = 0;
80
set_native_charset (const char *newset)
83
#ifdef HAVE_LANGINFO_CODESET
84
newset = nl_langinfo (CODESET);
89
if (strlen (newset) > 3 && !ascii_memcasecmp (newset, "iso", 3))
92
if (*newset == '-' || *newset == '_')
97
|| !ascii_strcasecmp (newset, "8859-1")
98
|| !ascii_strcasecmp (newset, "8859-15"))
100
active_charset_name = "iso-8859-1";
102
active_charset = NULL;
104
else if (!ascii_strcasecmp (newset, "8859-2"))
106
active_charset_name = "iso-8859-2";
108
active_charset = latin2_unicode;
110
else if (!ascii_strcasecmp (newset, "koi8-r"))
112
active_charset_name = "koi8-r";
114
active_charset = koi8_unicode;
116
else if (!ascii_strcasecmp (newset, "utf8")
117
|| !ascii_strcasecmp (newset, "utf-8"))
119
active_charset_name = "utf-8";
121
active_charset = NULL;
129
get_native_charset ()
131
return active_charset_name;
135
* Convert string, which is in native encoding to UTF8 and return the
136
* new allocated UTF8 string.
139
native_to_utf8 (const char *string)
148
buffer = jnlib_xstrdup (string);
150
else if (active_charset)
152
for (s = string; *s; s++)
156
length += 2; /* we may need 3 bytes */
158
buffer = jnlib_xmalloc (length + 1);
159
for (p = buffer, s = string; *s; s++)
163
ushort val = active_charset[*s & 0x7f];
166
*p++ = 0xc0 | ((val >> 6) & 0x1f);
167
*p++ = 0x80 | (val & 0x3f);
171
*p++ = 0xe0 | ((val >> 12) & 0x0f);
172
*p++ = 0x80 | ((val >> 6) & 0x3f);
173
*p++ = 0x80 | (val & 0x3f);
183
for (s = string; *s; s++)
189
buffer = jnlib_xmalloc (length + 1);
190
for (p = buffer, s = string; *s; s++)
194
*p++ = 0xc0 | ((*s >> 6) & 3);
195
*p++ = 0x80 | (*s & 0x3f);
206
/* Convert string, which is in UTF8 to native encoding. Replace
207
* illegal encodings by some "\xnn" and quote all control
208
* characters. A character with value DELIM will always be quoted, it
209
* must be a vanilla ASCII character. */
211
utf8_to_native (const char *string, size_t length, int delim)
219
byte *buffer = NULL, *p = NULL;
220
unsigned long val = 0;
224
/* 1. pass (p==NULL): count the extended utf-8 characters */
225
/* 2. pass (p!=NULL): create string */
228
for (slen = length, nleft = encidx = 0, n = 0, s = string; slen;
233
if (!(*s < 128 || (*s >= 0xc0 && *s <= 0xfd)))
238
sprintf (p, "\\x%02x", *s);
250
if (*s < 0x20 || *s == 0x7f || *s == delim ||
251
(delim && *s == '\\'))
292
sprintf (p, "x%02x", *s);
305
else if ((*s & 0xe0) == 0xc0)
310
encbuf[encidx++] = *s;
312
else if ((*s & 0xf0) == 0xe0)
317
encbuf[encidx++] = *s;
319
else if ((*s & 0xf8) == 0xf0)
324
encbuf[encidx++] = *s;
326
else if ((*s & 0xfc) == 0xf8)
331
encbuf[encidx++] = *s;
333
else if ((*s & 0xfe) == 0xfc)
338
encbuf[encidx++] = *s;
341
{ /* invalid encoding: print as \xnn */
344
sprintf (p, "\\x%02x", *s);
351
else if (*s < 0x80 || *s >= 0xc0)
355
for (i = 0; i < encidx; i++)
357
sprintf (p, "\\x%02x", encbuf[i]);
360
sprintf (p, "\\x%02x", *s);
370
encbuf[encidx++] = *s;
379
for (i = 0; i < encidx; i++)
385
else if (active_charset)
387
for (i = 0; i < 128; i++)
389
if (active_charset[i] == val)
393
{ /* we can print this one */
399
{ /* we do not have a translation: print utf8 */
402
for (i = 0; i < encidx; i++)
404
sprintf (p, "\\x%02x", encbuf[i]);
414
if (val >= 0x80 && val < 256)
416
n++; /* we can simply print this character */
421
{ /* we do not have a translation: print utf8 */
424
for (i = 0; i < encidx; i++)
426
sprintf (p, "\\x%02x", encbuf[i]);
439
{ /* allocate the buffer after the first pass */
440
buffer = p = jnlib_xmalloc (n + 1);
444
*p = 0; /* make a string */