This source file includes following definitions.
- FUNC
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 #include <config.h>
27
28
29 #include "unistr.h"
30
31 #define FUNC u32_to_u16
32 #define SRC_UNIT uint32_t
33 #define DST_UNIT uint16_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 {
42 const SRC_UNIT *s_end = s + n;
43
44 DST_UNIT *result;
45 size_t allocated;
46 size_t length;
47
48 if (resultbuf != NULL)
49 {
50 result = resultbuf;
51 allocated = *lengthp;
52 }
53 else
54 {
55 result = NULL;
56 allocated = 0;
57 }
58 length = 0;
59
60
61
62
63 while (s < s_end)
64 {
65 ucs4_t uc;
66 int count;
67
68
69 uc = *s++;
70
71
72
73
74 count = u16_uctomb (result + length, uc, allocated - length);
75 if (count == -1)
76 {
77 if (!(result == resultbuf || result == NULL))
78 free (result);
79 errno = EILSEQ;
80 return NULL;
81 }
82 if (count == -2)
83 {
84 DST_UNIT *memory;
85
86 allocated = (allocated > 0 ? 2 * allocated : 12);
87 if (length + 2 > allocated)
88 allocated = length + 2;
89 if (result == resultbuf || result == NULL)
90 memory = (DST_UNIT *) malloc (allocated * sizeof (DST_UNIT));
91 else
92 memory =
93 (DST_UNIT *) realloc (result, allocated * sizeof (DST_UNIT));
94
95 if (memory == NULL)
96 {
97 if (!(result == resultbuf || result == NULL))
98 free (result);
99 errno = ENOMEM;
100 return NULL;
101 }
102 if (result == resultbuf && length > 0)
103 memcpy ((char *) memory, (char *) result,
104 length * sizeof (DST_UNIT));
105 result = memory;
106 count = u16_uctomb (result + length, uc, allocated - length);
107 if (count < 0)
108 abort ();
109 }
110 length += count;
111 }
112
113 if (length == 0)
114 {
115 if (result == NULL)
116 {
117
118 result = (DST_UNIT *) malloc (1);
119 if (result == NULL)
120 {
121 errno = ENOMEM;
122 return NULL;
123 }
124 }
125 }
126 else if (result != resultbuf && length < allocated)
127 {
128
129 DST_UNIT *memory;
130
131 memory = (DST_UNIT *) realloc (result, length * sizeof (DST_UNIT));
132 if (memory != NULL)
133 result = memory;
134 }
135
136 *lengthp = length;
137 return result;
138 }