1 /* Conversion from UTF-16/UTF-32 to 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 *tocode,
21 enum iconv_ilseq_handler handler,
22 const UNIT *src, size_t srclen,
24 char **resultp, size_t *lengthp)
27 size_t *scaled_offsets;
33 (size_t *) malloc (srclen * sizeof (UNIT) * sizeof (size_t));
34 if (scaled_offsets == NULL)
41 scaled_offsets = NULL;
43 retval = mem_iconveha ((const char *) src, srclen * sizeof (UNIT),
45 handler == iconveh_question_mark, handler,
46 scaled_offsets, resultp, lengthp);
52 /* Convert scaled_offsets[srclen * sizeof (UNIT)] to
56 for (i = 0; i < srclen; i++)
57 offsets[i] = scaled_offsets[i * sizeof (UNIT)];
59 free (scaled_offsets);
64 size_t tmpbufsize = SIZEOF (tmpbuf);
67 size_t *scaled_offsets;
70 utf8_src = U_TO_U8 (src, srclen, tmpbuf, &tmpbufsize);
73 utf8_srclen = tmpbufsize;
77 scaled_offsets = (size_t *) malloc (utf8_srclen * sizeof (size_t));
78 if (scaled_offsets == NULL)
80 if (utf8_src != tmpbuf)
87 scaled_offsets = NULL;
89 retval = u8_conv_to_encoding (tocode, handler, utf8_src, utf8_srclen,
90 scaled_offsets, resultp, lengthp);
93 if (utf8_src != tmpbuf)
95 int saved_errno = errno;
103 size_t iunit; /* offset into src */
104 size_t i8; /* offset into utf8_src */
106 for (iunit = 0; iunit < srclen; iunit++)
107 offsets[iunit] = (size_t)(-1);
111 while (iunit < srclen)
116 offsets[iunit] = scaled_offsets[i8];
118 countunit = U_MBLEN (src + iunit, srclen - iunit);
119 count8 = u8_mblen (utf8_src + i8, utf8_srclen - i8);
120 if (countunit < 0 || count8 < 0)
125 free (scaled_offsets);
127 if (utf8_src != tmpbuf)