Hash :
12dbf71b
Author :
Date :
2002-05-06T10:21:11
Fix a bug in the CP1255, CP1258, TCVN decoders: The base characters of combining characters could be dropped at the end of the conversion buffer.
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
/*
* Copyright (C) 2000-2002 Free Software Foundation, Inc.
* This file is part of the GNU LIBICONV Library.
*
* The GNU LIBICONV Library 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
* of the License, or (at your option) any later version.
*
* The GNU LIBICONV Library 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 the GNU LIBICONV Library; see the file COPYING.LIB.
* If not, write to the Free Software Foundation, Inc., 59 Temple Place -
* Suite 330, Boston, MA 02111-1307, USA.
*/
/* This file defines three conversion loops:
- from wchar_t to anything else,
- from anything else to wchar_t,
- from wchar_t to wchar_t.
*/
#if HAVE_WCRTOMB || HAVE_MBRTOWC
# include <wchar.h>
# define BUF_SIZE 64 /* assume MB_LEN_MAX <= 64 */
/* Some systems, like BeOS, have multibyte encodings but lack mbstate_t. */
extern size_t mbrtowc ();
# ifdef mbstate_t
# define mbrtowc(pwc, s, n, ps) (mbrtowc)(pwc, s, n, 0)
# define mbsinit(ps) 1
# endif
# ifndef mbsinit
# if !HAVE_MBSINIT
# define mbsinit(ps) 1
# endif
# endif
#else
# ifndef mbstate_t
typedef int mbstate_t;
# endif
#endif
/*
* The first two conversion loops have an extended conversion descriptor.
*/
struct wchar_conv_struct {
struct conv_struct parent;
mbstate_t state;
};
#if HAVE_WCRTOMB
/* From wchar_t to anything else. */
static size_t wchar_from_loop_convert (iconv_t icd,
const char* * inbuf, size_t *inbytesleft,
char* * outbuf, size_t *outbytesleft)
{
struct wchar_conv_struct * wcd = (struct wchar_conv_struct *) icd;
size_t result = 0;
while (*inbytesleft >= sizeof(wchar_t)) {
const wchar_t * inptr = (const wchar_t *) *inbuf;
size_t inleft = *inbytesleft;
char buf[BUF_SIZE];
mbstate_t state = wcd->state;
size_t bufcount = 0;
while (inleft >= sizeof(wchar_t)) {
/* Convert one wchar_t to multibyte representation. */
size_t count = wcrtomb(buf+bufcount,*inptr,&state);
if (count == (size_t)(-1)) {
/* Invalid input. */
if (!wcd->parent.discard_ilseq) {
errno = EILSEQ;
return -1;
}
count = 0;
}
inptr++;
inleft -= sizeof(wchar_t);
bufcount += count;
if (count == 0) {
/* Continue, append next wchar_t. */
} else {
/* Attempt to convert the accumulated multibyte representations
to the target encoding. */
const char* bufptr = buf;
size_t bufleft = bufcount;
char* outptr = *outbuf;
size_t outleft = *outbytesleft;
size_t res = unicode_loop_convert(&wcd->parent,
&bufptr,&bufleft,
&outptr,&outleft);
if (res == (size_t)(-1)) {
if (errno == EILSEQ)
/* Invalid input. */
return -1;
else if (errno == E2BIG)
/* Output buffer too small. */
return -1;
else if (errno == EINVAL) {
/* Continue, append next wchar_t, but avoid buffer overrun. */
if (bufcount + MB_CUR_MAX > BUF_SIZE)
abort();
} else
abort();
} else {
/* Successful conversion. */
wcd->state = state;
*inbuf = (const char *) inptr;
*inbytesleft = inleft;
*outbuf = outptr;
*outbytesleft = outleft;
result += res;
break;
}
}
}
}
return result;
}
static size_t wchar_from_loop_reset (iconv_t icd,
char* * outbuf, size_t *outbytesleft)
{
struct wchar_conv_struct * wcd = (struct wchar_conv_struct *) icd;
if (outbuf == NULL || *outbuf == NULL) {
/* Reset the states. */
memset(&wcd->state,'\0',sizeof(mbstate_t));
return unicode_loop_reset(&wcd->parent,NULL,NULL);
} else {
if (!mbsinit(&wcd->state)) {
mbstate_t state = wcd->state;
char buf[BUF_SIZE];
size_t bufcount = wcrtomb(buf,(wchar_t)0,&state);
if (bufcount == (size_t)(-1) || bufcount == 0 || buf[bufcount-1] != '\0')
abort();
else {
const char* bufptr = buf;
size_t bufleft = bufcount-1;
char* outptr = *outbuf;
size_t outleft = *outbytesleft;
size_t res = unicode_loop_convert(&wcd->parent,
&bufptr,&bufleft,
&outptr,&outleft);
if (res == (size_t)(-1)) {
if (errno == E2BIG)
return -1;
else
abort();
} else {
res = unicode_loop_reset(&wcd->parent,&outptr,&outleft);
if (res == (size_t)(-1))
return res;
else {
/* Successful. */
wcd->state = state;
*outbuf = outptr;
*outbytesleft = outleft;
return 0;
}
}
}
} else
return unicode_loop_reset(&wcd->parent,outbuf,outbytesleft);
}
}
#endif
#if HAVE_MBRTOWC
/* From anything else to wchar_t. */
static size_t wchar_to_loop_convert (iconv_t icd,
const char* * inbuf, size_t *inbytesleft,
char* * outbuf, size_t *outbytesleft)
{
struct wchar_conv_struct * wcd = (struct wchar_conv_struct *) icd;
size_t result = 0;
while (*inbytesleft > 0) {
size_t incount;
for (incount = 1; incount <= *inbytesleft; incount++) {
char buf[BUF_SIZE];
const char* inptr = *inbuf;
size_t inleft = incount;
char* bufptr = buf;
size_t bufleft = BUF_SIZE;
size_t res = unicode_loop_convert(&wcd->parent,
&inptr,&inleft,
&bufptr,&bufleft);
if (res == (size_t)(-1)) {
if (errno == EILSEQ)
/* Invalid input. */
return -1;
else if (errno == EINVAL) {
/* Incomplete input. Next try with one more input byte. */
} else
/* E2BIG shouldn't occur. */
abort();
} else {
/* Successful conversion. */
size_t bufcount = bufptr-buf; /* = BUF_SIZE-bufleft */
mbstate_t state = wcd->state;
wchar_t wc;
res = mbrtowc(&wc,buf,bufcount,&state);
if (res == (size_t)(-2)) {
/* Next try with one more input byte. */
} else {
if (res == (size_t)(-1)) {
/* Invalid input. */
if (!wcd->parent.discard_ilseq)
return -1;
} else {
if (*outbytesleft < sizeof(wchar_t)) {
errno = E2BIG;
return -1;
}
*(wchar_t*) *outbuf = wc;
/* Restoring the state is not needed because it is the initial
state anyway: For all known locale encodings, the multibyte
to wchar_t conversion doesn't have shift state, and we have
excluded partial accumulated characters. */
/* wcd->state = state; */
*outbuf += sizeof(wchar_t);
*outbytesleft -= sizeof(wchar_t);
}
*inbuf += incount;
*inbytesleft -= incount;
result += res;
break;
}
}
}
}
return result;
}
static size_t wchar_to_loop_reset (iconv_t icd,
char* * outbuf, size_t *outbytesleft)
{
struct wchar_conv_struct * wcd = (struct wchar_conv_struct *) icd;
size_t res = unicode_loop_reset(&wcd->parent,outbuf,outbytesleft);
if (res == (size_t)(-1))
return res;
memset(&wcd->state,0,sizeof(mbstate_t));
return 0;
}
#endif
/* From wchar_t to wchar_t. */
static size_t wchar_id_loop_convert (iconv_t icd,
const char* * inbuf, size_t *inbytesleft,
char* * outbuf, size_t *outbytesleft)
{
const wchar_t* inptr = (const wchar_t*) *inbuf;
size_t inleft = *inbytesleft / sizeof(wchar_t);
wchar_t* outptr = (wchar_t*) *outbuf;
size_t outleft = *outbytesleft / sizeof(wchar_t);
size_t count = (inleft <= outleft ? inleft : outleft);
if (count > 0) {
*inbytesleft -= count * sizeof(wchar_t);
*outbytesleft -= count * sizeof(wchar_t);
do
*outptr++ = *inptr++;
while (--count > 0);
*inbuf = (const char*) inptr;
*outbuf = (char*) outptr;
}
return 0;
}
static size_t wchar_id_loop_reset (iconv_t icd,
char* * outbuf, size_t *outbytesleft)
{
return 0;
}