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 u16_to_u32
32 #define SRC_UNIT uint16_t
33 #define DST_UNIT uint32_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 = u16_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 if (length + 1 > allocated)
81 {
82 DST_UNIT *memory;
83
84 allocated = (allocated > 0 ? 2 * allocated : 12);
85 if (length + 1 > allocated)
86 allocated = length + 1;
87 if (result == resultbuf || result == NULL)
88 memory = (DST_UNIT *) malloc (allocated * sizeof (DST_UNIT));
89 else
90 memory =
91 (DST_UNIT *) realloc (result, allocated * sizeof (DST_UNIT));
92
93 if (memory == NULL)
94 {
95 if (!(result == resultbuf || result == NULL))
96 free (result);
97 errno = ENOMEM;
98 return NULL;
99 }
100 if (result == resultbuf && length > 0)
101 memcpy ((char *) memory, (char *) result,
102 length * sizeof (DST_UNIT));
103 result = memory;
104 }
105 result[length++] = uc;
106 }
107
108 if (length == 0)
109 {
110 if (result == NULL)
111 {
112
113 result = (DST_UNIT *) malloc (1);
114 if (result == NULL)
115 {
116 errno = ENOMEM;
117 return NULL;
118 }
119 }
120 }
121 else if (result != resultbuf && length < allocated)
122 {
123
124 DST_UNIT *memory;
125
126 memory = (DST_UNIT *) realloc (result, length * sizeof (DST_UNIT));
127 if (memory != NULL)
128 result = memory;
129 }
130
131 *lengthp = length;
132 return result;
133 }