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 Library General Public License as published
6 by the Free Software Foundation; either version 2, or (at your option)
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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 FUNC (const char *fromcode,
21 enum iconv_ilseq_handler handler,
22 const char *src, size_t srclen,
24 UNIT **resultp, size_t *lengthp)
29 if (mem_iconveha (src, srclen, fromcode, UTF_NAME, true, handler,
30 offsets, (char **) resultp, &length) < 0)
34 /* Convert 'char *' offsets to 'UNIT *' offsets. */
35 size_t *offsets_end = offsets + srclen;
38 for (o = offsets; o < offsets_end; o++)
39 if (*o != (size_t)(-1))
40 *o = *o / sizeof (UNIT);
42 if ((length % sizeof (UNIT)) != 0)
44 *lengthp = length / sizeof (UNIT);
47 uint8_t *utf8_string = NULL;
48 size_t utf8_length = 0;
51 if (u8_conv_from_encoding (fromcode, handler, src, srclen, offsets,
52 &utf8_string, &utf8_length) < 0)
54 if (utf8_string == NULL)
59 result = U8_TO_U (utf8_string, utf8_length, *resultp, lengthp);
62 int saved_errno = errno;
69 size_t length = *lengthp;
70 size_t *offsets_end = offsets + srclen;
72 size_t off8 = 0; /* offset into utf8_string */
73 size_t offunit = 0; /* offset into result */
75 for (o = offsets; o < offsets_end; o++)
76 if (*o != (size_t)(-1))
80 int count8 = u8_mblen (utf8_string + off8, utf8_length - off8);
81 int countunit = U_MBLEN (result + offunit, length - offunit);
82 if (count8 < 0 || countunit < 0)