1 /* Conversion to UTF-16/UTF-32 from legacy encodings.
2 Copyright (C) 2002, 2006-2007 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as published
6 by the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 FUNC (const char *fromcode,
19 enum iconv_ilseq_handler handler,
20 const char *src, size_t srclen,
22 UNIT **resultp, size_t *lengthp)
27 if (mem_iconveha (src, srclen, fromcode, UTF_NAME, true, handler,
28 offsets, (char **) resultp, &length) < 0)
32 /* Convert 'char *' offsets to 'UNIT *' offsets. */
33 size_t *offsets_end = offsets + srclen;
36 for (o = offsets; o < offsets_end; o++)
37 if (*o != (size_t)(-1))
38 *o = *o / sizeof (UNIT);
40 if ((length % sizeof (UNIT)) != 0)
42 *lengthp = length / sizeof (UNIT);
45 uint8_t *utf8_string = NULL;
46 size_t utf8_length = 0;
49 if (u8_conv_from_encoding (fromcode, handler, src, srclen, offsets,
50 &utf8_string, &utf8_length) < 0)
52 if (utf8_string == NULL)
57 result = U8_TO_U (utf8_string, utf8_length, *resultp, lengthp);
60 int saved_errno = errno;
67 size_t length = *lengthp;
68 size_t *offsets_end = offsets + srclen;
70 size_t off8 = 0; /* offset into utf8_string */
71 size_t offunit = 0; /* offset into result */
73 for (o = offsets; o < offsets_end; o++)
74 if (*o != (size_t)(-1))
78 int count8 = u8_mblen (utf8_string + off8, utf8_length - off8);
79 int countunit = U_MBLEN (result + offunit, length - offunit);
80 if (count8 < 0 || countunit < 0)