1 /* Conversion from UTF-16/UTF-32 to legacy encodings.
2 Copyright (C) 2002, 2006-2008 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 *tocode,
19 enum iconv_ilseq_handler handler,
20 const UNIT *src, size_t srclen,
22 char **resultp, size_t *lengthp)
25 size_t *scaled_offsets;
31 (size_t *) malloc (srclen * sizeof (UNIT) * sizeof (size_t));
32 if (scaled_offsets == NULL)
39 scaled_offsets = NULL;
41 retval = mem_iconveha ((const char *) src, srclen * sizeof (UNIT),
43 handler == iconveh_question_mark, handler,
44 scaled_offsets, resultp, lengthp);
50 /* Convert scaled_offsets[srclen * sizeof (UNIT)] to
54 for (i = 0; i < srclen; i++)
55 offsets[i] = scaled_offsets[i * sizeof (UNIT)];
57 free (scaled_offsets);
62 size_t tmpbufsize = SIZEOF (tmpbuf);
65 size_t *scaled_offsets;
68 utf8_src = U_TO_U8 (src, srclen, tmpbuf, &tmpbufsize);
71 utf8_srclen = tmpbufsize;
75 scaled_offsets = (size_t *) malloc (utf8_srclen * sizeof (size_t));
76 if (scaled_offsets == NULL)
78 if (utf8_src != tmpbuf)
85 scaled_offsets = NULL;
87 retval = u8_conv_to_encoding (tocode, handler, utf8_src, utf8_srclen,
88 scaled_offsets, resultp, lengthp);
91 if (utf8_src != tmpbuf)
93 int saved_errno = errno;
101 size_t iunit; /* offset into src */
102 size_t i8; /* offset into utf8_src */
104 for (iunit = 0; iunit < srclen; iunit++)
105 offsets[iunit] = (size_t)(-1);
109 while (iunit < srclen)
114 offsets[iunit] = scaled_offsets[i8];
116 countunit = U_MBLEN (src + iunit, srclen - iunit);
117 count8 = u8_mblen (utf8_src + i8, utf8_srclen - i8);
118 if (countunit < 0 || count8 < 0)
123 free (scaled_offsets);
125 if (utf8_src != tmpbuf)