1 /* Convert UTF-16 string to UTF-32 string.
2 Copyright (C) 2002, 2006-2007, 2009-2014 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2002.
5 This program is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Lesser General Public License as published
7 by the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #define FUNC u16_to_u32
24 #define SRC_UNIT uint16_t
25 #define DST_UNIT uint32_t
32 FUNC (const SRC_UNIT *s, size_t n, DST_UNIT *resultbuf, size_t *lengthp)
34 const SRC_UNIT *s_end = s + n;
35 /* Output string accumulator. */
40 if (resultbuf != NULL)
52 result is either == resultbuf or == NULL or malloc-allocated.
53 If length > 0, then result != NULL. */
60 /* Fetch a Unicode character from the input string. */
61 count = u16_mbtoucr (&uc, s, s_end - s);
64 if (!(result == resultbuf || result == NULL))
71 /* Store it in the output string. */
72 if (length + 1 > allocated)
76 allocated = (allocated > 0 ? 2 * allocated : 12);
77 if (length + 1 > allocated)
78 allocated = length + 1;
79 if (result == resultbuf || result == NULL)
80 memory = (DST_UNIT *) malloc (allocated * sizeof (DST_UNIT));
83 (DST_UNIT *) realloc (result, allocated * sizeof (DST_UNIT));
87 if (!(result == resultbuf || result == NULL))
92 if (result == resultbuf && length > 0)
93 memcpy ((char *) memory, (char *) result,
94 length * sizeof (DST_UNIT));
97 result[length++] = uc;
104 /* Return a non-NULL value. NULL means error. */
105 result = (DST_UNIT *) malloc (1);
113 else if (result != resultbuf && length < allocated)
115 /* Shrink the allocated memory if possible. */
118 memory = (DST_UNIT *) realloc (result, length * sizeof (DST_UNIT));