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