/src/libunistring/lib/unistr/u16-to-u8.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Convert UTF-16 string to UTF-8 string. |
2 | | Copyright (C) 2002, 2006-2007, 2009-2024 Free Software Foundation, Inc. |
3 | | Written by Bruno Haible <bruno@clisp.org>, 2002. |
4 | | |
5 | | This file is free software. |
6 | | It is dual-licensed under "the GNU LGPLv3+ or the GNU GPLv2+". |
7 | | You can redistribute it and/or modify it under either |
8 | | - the terms of the GNU Lesser General Public License as published |
9 | | by the Free Software Foundation, either version 3, or (at your |
10 | | option) any later version, or |
11 | | - the terms of the GNU General Public License as published by the |
12 | | Free Software Foundation; either version 2, or (at your option) |
13 | | any later version, or |
14 | | - the same dual license "the GNU LGPLv3+ or the GNU GPLv2+". |
15 | | |
16 | | This file is distributed in the hope that it will be useful, |
17 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
19 | | Lesser General Public License and the GNU General Public License |
20 | | for more details. |
21 | | |
22 | | You should have received a copy of the GNU Lesser General Public |
23 | | License and of the GNU General Public License along with this |
24 | | program. If not, see <https://www.gnu.org/licenses/>. */ |
25 | | |
26 | | #include <config.h> |
27 | | |
28 | | /* Specification. */ |
29 | | #include "unistr.h" |
30 | | |
31 | | #define FUNC u16_to_u8 |
32 | | #define SRC_UNIT uint16_t |
33 | 34.1k | #define DST_UNIT uint8_t |
34 | | |
35 | | #include <errno.h> |
36 | | #include <stdlib.h> |
37 | | #include <string.h> |
38 | | |
39 | | DST_UNIT * |
40 | | FUNC (const SRC_UNIT *s, size_t n, DST_UNIT *resultbuf, size_t *lengthp) |
41 | 12.9k | { |
42 | 12.9k | const SRC_UNIT *s_end = s + n; |
43 | | /* Output string accumulator. */ |
44 | 12.9k | DST_UNIT *result; |
45 | 12.9k | size_t allocated; |
46 | 12.9k | size_t length; |
47 | | |
48 | 12.9k | if (resultbuf != NULL) |
49 | 0 | { |
50 | 0 | result = resultbuf; |
51 | 0 | allocated = *lengthp; |
52 | 0 | } |
53 | 12.9k | else |
54 | 12.9k | { |
55 | 12.9k | result = NULL; |
56 | 12.9k | allocated = 0; |
57 | 12.9k | } |
58 | 12.9k | length = 0; |
59 | | /* Invariants: |
60 | | result is either == resultbuf or == NULL or malloc-allocated. |
61 | | If length > 0, then result != NULL. */ |
62 | | |
63 | 59.0k | while (s < s_end) |
64 | 50.1k | { |
65 | 50.1k | ucs4_t uc; |
66 | 50.1k | int count; |
67 | | |
68 | | /* Fetch a Unicode character from the input string. */ |
69 | 50.1k | count = u16_mbtoucr (&uc, s, s_end - s); |
70 | 50.1k | if (count < 0) |
71 | 4.06k | { |
72 | 4.06k | if (!(result == resultbuf || result == NULL)) |
73 | 1.46k | free (result); |
74 | 4.06k | errno = EILSEQ; |
75 | 4.06k | return NULL; |
76 | 4.06k | } |
77 | 46.0k | s += count; |
78 | | |
79 | | /* Store it in the output string. */ |
80 | 46.0k | count = u8_uctomb (result + length, uc, allocated - length); |
81 | 46.0k | if (count == -1) |
82 | 0 | { |
83 | 0 | if (!(result == resultbuf || result == NULL)) |
84 | 0 | free (result); |
85 | 0 | errno = EILSEQ; |
86 | 0 | return NULL; |
87 | 0 | } |
88 | 46.0k | if (count == -2) |
89 | 13.2k | { |
90 | 13.2k | DST_UNIT *memory; |
91 | | |
92 | 13.2k | allocated = (allocated > 0 ? 2 * allocated : 12); |
93 | 13.2k | if (length + 6 > allocated) |
94 | 0 | allocated = length + 6; |
95 | 13.2k | if (result == resultbuf || result == NULL) |
96 | 9.82k | memory = (DST_UNIT *) malloc (allocated * sizeof (DST_UNIT)); |
97 | 3.45k | else |
98 | 3.45k | memory = |
99 | 3.45k | (DST_UNIT *) realloc (result, allocated * sizeof (DST_UNIT)); |
100 | | |
101 | 13.2k | if (memory == NULL) |
102 | 0 | { |
103 | 0 | if (!(result == resultbuf || result == NULL)) |
104 | 0 | free (result); |
105 | 0 | errno = ENOMEM; |
106 | 0 | return NULL; |
107 | 0 | } |
108 | 13.2k | if (result == resultbuf && length > 0) |
109 | 0 | memcpy ((char *) memory, (char *) result, |
110 | 0 | length * sizeof (DST_UNIT)); |
111 | 13.2k | result = memory; |
112 | 13.2k | count = u8_uctomb (result + length, uc, allocated - length); |
113 | 13.2k | if (count < 0) |
114 | 0 | abort (); |
115 | 13.2k | } |
116 | 46.0k | length += count; |
117 | 46.0k | } |
118 | | |
119 | 8.89k | if (length == 0) |
120 | 533 | { |
121 | 533 | if (result == NULL) |
122 | 533 | { |
123 | | /* Return a non-NULL value. NULL means error. */ |
124 | 533 | result = (DST_UNIT *) malloc (1); |
125 | 533 | if (result == NULL) |
126 | 0 | { |
127 | 0 | errno = ENOMEM; |
128 | 0 | return NULL; |
129 | 0 | } |
130 | 533 | } |
131 | 533 | } |
132 | 8.35k | else if (result != resultbuf && length < allocated) |
133 | 7.88k | { |
134 | | /* Shrink the allocated memory if possible. */ |
135 | 7.88k | DST_UNIT *memory; |
136 | | |
137 | 7.88k | memory = (DST_UNIT *) realloc (result, length * sizeof (DST_UNIT)); |
138 | 7.88k | if (memory != NULL) |
139 | 7.88k | result = memory; |
140 | 7.88k | } |
141 | | |
142 | 8.89k | *lengthp = length; |
143 | 8.89k | return result; |
144 | 8.89k | } |