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 u8_to_u16
32 #define SRC_UNIT uint8_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 count = u8_mbtoucr (&uc, s, s_end - s);
70 if (count < 0)
71 {
72 if (!(result == resultbuf || result == NULL))
73 free (result);
74 errno = EILSEQ;
75 return NULL;
76 }
77 s += count;
78
79
80 count = u16_uctomb (result + length, uc, allocated - length);
81 if (count == -1)
82 {
83 if (!(result == resultbuf || result == NULL))
84 free (result);
85 errno = EILSEQ;
86 return NULL;
87 }
88 if (count == -2)
89 {
90 DST_UNIT *memory;
91
92 allocated = (allocated > 0 ? 2 * allocated : 12);
93 if (length + 2 > allocated)
94 allocated = length + 2;
95 if (result == resultbuf || result == NULL)
96 memory = (DST_UNIT *) malloc (allocated * sizeof (DST_UNIT));
97 else
98 memory =
99 (DST_UNIT *) realloc (result, allocated * sizeof (DST_UNIT));
100
101 if (memory == NULL)
102 {
103 if (!(result == resultbuf || result == NULL))
104 free (result);
105 errno = ENOMEM;
106 return NULL;
107 }
108 if (result == resultbuf && length > 0)
109 memcpy ((char *) memory, (char *) result,
110 length * sizeof (DST_UNIT));
111 result = memory;
112 count = u16_uctomb (result + length, uc, allocated - length);
113 if (count < 0)
114 abort ();
115 }
116 length += count;
117 }
118
119 if (length == 0)
120 {
121 if (result == NULL)
122 {
123
124 result = (DST_UNIT *) malloc (1);
125 if (result == NULL)
126 {
127 errno = ENOMEM;
128 return NULL;
129 }
130 }
131 }
132 else if (result != resultbuf && length < allocated)
133 {
134
135 DST_UNIT *memory;
136
137 memory = (DST_UNIT *) realloc (result, length * sizeof (DST_UNIT));
138 if (memory != NULL)
139 result = memory;
140 }
141
142 *lengthp = length;
143 return result;
144 }