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 u8_to_u32
24 #define SRC_UNIT uint8_t
25 #define DST_UNIT uint32_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 count = u8_mbtoucr (&uc, s, s_end - s);
62 if (count < 0)
63 {
64 if (!(result == resultbuf || result == NULL))
65 free (result);
66 errno = EILSEQ;
67 return NULL;
68 }
69 s += count;
70
71
72 if (length + 1 > allocated)
73 {
74 DST_UNIT *memory;
75
76 allocated = (allocated > 0 ? 2 * allocated : 12);
77 if (length + 1 > allocated)
78 allocated = length + 1;
79 if (result == resultbuf || result == NULL)
80 memory = (DST_UNIT *) malloc (allocated * sizeof (DST_UNIT));
81 else
82 memory =
83 (DST_UNIT *) realloc (result, allocated * sizeof (DST_UNIT));
84
85 if (memory == NULL)
86 {
87 if (!(result == resultbuf || result == NULL))
88 free (result);
89 errno = ENOMEM;
90 return NULL;
91 }
92 if (result == resultbuf && length > 0)
93 memcpy ((char *) memory, (char *) result,
94 length * sizeof (DST_UNIT));
95 result = memory;
96 }
97 result[length++] = uc;
98 }
99
100 if (length == 0)
101 {
102 if (result == NULL)
103 {
104
105 result = (DST_UNIT *) malloc (1);
106 if (result == NULL)
107 {
108 errno = ENOMEM;
109 return NULL;
110 }
111 }
112 }
113 else if (result != resultbuf && length < allocated)
114 {
115
116 DST_UNIT *memory;
117
118 memory = (DST_UNIT *) realloc (result, length * sizeof (DST_UNIT));
119 if (memory != NULL)
120 result = memory;
121 }
122
123 *lengthp = length;
124 return result;
125 }