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
|
/* OS/2 iconv() implementation through OS/2 Unicode API
Copyright (C) 2001-2002 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published
by the Free Software Foundation; either version 2, 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
USA. */
/*
This file implements an iconv wrapper based on OS/2 Unicode API.
*/
#include <uconv.h>
typedef struct _iconv_t
{
UconvObject from; /* "From" conversion handle */
UconvObject to; /* "To" conversion handle */
} *iconv_t;
/* Tell "iconv.h" to not define iconv_t by itself. */
#define _ICONV_T
#include "iconv.h"
#include <errno.h>
#include <alloca.h>
/* Convert an encoding name to te form understood by UniCreateUconvObject. */
static inline void
cp_convert (const char *cp, UniChar *ucp)
{
size_t sl = 0;
if (!strcasecmp (cp, "EUC-JP"))
memcpy (ucp, L"IBM-954", 8*2);
else if (!strcasecmp (cp, "EUC-KR"))
memcpy (ucp, L"IBM-970", 8*2);
else if (!strcasecmp (cp, "EUC-TW"))
memcpy (ucp, L"IBM-964", 8*2);
else if (!strcasecmp (cp, "EUC-CN"))
memcpy (ucp, L"IBM-1383", 9*2);
else
{
/* Transform CPXXX naming style to IBM-XXX style */
if ((cp[0] == 'C' || cp[0] == 'c') && (cp[1] == 'P' || cp[1] == 'p'))
{
ucp[sl++] = 'I';
ucp[sl++] = 'B';
ucp[sl++] = 'M';
ucp[sl++] = '-';
cp += 2;
}
while (*cp != '\0')
ucp[sl++] = *cp++;
ucp[sl] = 0;
}
}
iconv_t
iconv_open (const char *cp_to, const char *cp_from)
{
UniChar *ucp;
iconv_t conv;
uconv_attribute_t attr;
conv = (iconv_t) malloc (sizeof (struct _iconv_t));
if (conv == NULL)
{
errno = ENOMEM;
return (iconv_t)(-1);
}
ucp = (UniChar *) alloca ((strlen (cp_from) + 2 + 1) * sizeof (UniChar));
cp_convert (cp_from, ucp);
if (UniCreateUconvObject (ucp, &conv->from))
{
free (conv);
errno = EINVAL;
return (iconv_t)(-1);
}
ucp = (UniChar *) alloca ((strlen (cp_to) + 2 + 1) * sizeof (UniChar));
cp_convert (cp_to, ucp);
if (UniCreateUconvObject (ucp, &conv->to))
{
UniFreeUconvObject (conv->from);
free (conv);
errno = EINVAL;
return (iconv_t)(-1);
}
UniQueryUconvObject (conv->from, &attr, sizeof (attr), NULL, NULL, NULL);
/* Do not treat 0x7f as a control character
(don't understand what it exactly means but without it MBCS prefix
character detection sometimes could fail (when 0x7f is a prefix)).
And don't treat the string as a path (the docs also don't explain
what it exactly means, but I'm pretty sure converted texts will
mostly not be paths). */
attr.converttype &= ~(CVTTYPE_CTRL7F | CVTTYPE_PATH);
UniSetUconvObject (conv->from, &attr);
return conv;
}
size_t
iconv (iconv_t conv,
const char **in, size_t *in_left,
char **out, size_t *out_left)
{
int rc;
size_t sl = *in_left, nonid;
UniChar *ucs = (UniChar *) alloca (sl * sizeof (UniChar));
UniChar *orig_ucs = ucs;
size_t retval = 0;
rc = UniUconvToUcs (conv->from, (void **)in, in_left, &ucs, &sl, &retval);
if (rc)
goto error;
sl = ucs - orig_ucs;
ucs = orig_ucs;
/* UniUconvFromUcs will stop at first NULL byte
while we want ALL the bytes converted. */
#if 1
rc = UniUconvFromUcs (conv->to, &ucs, &sl, (void **)out, out_left, &nonid);
if (rc)
goto error;
retval += nonid;
#else
while (sl)
{
size_t usl = 0;
while (sl && (ucs[usl] != 0))
usl++, sl--;
rc = UniUconvFromUcs (conv->to, &ucs, &usl, (void **)out, out_left, &nonid);
if (rc)
goto error;
retval += nonid;
if (sl && *out_left)
{
*(*out)++ = 0;
(*out_left)--;
ucs++; sl--;
}
}
#endif
return 0;
error:
/* Convert OS/2 error code to errno. */
switch (rc)
{
case ULS_ILLEGALSEQUENCE:
errno = EILSEQ;
break;
case ULS_INVALID:
errno = EINVAL;
break;
case ULS_BUFFERFULL:
errno = E2BIG;
break;
default:
errno = EBADF;
break;
}
return (size_t)(-1);
}
int
iconv_close (iconv_t conv)
{
if (conv != (iconv_t)(-1))
{
UniFreeUconvObject (conv->to);
UniFreeUconvObject (conv->from);
free (conv);
}
return 0;
}
|