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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
#include "converter.h"
#include "utfconverter.h"
#include "uniconv.h"
#include "unicode.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
struct utfconverter {
struct converter base;
};
static int
utf8_encode(struct converter *conv,
const uc_char_t **inbuf,
size_t inleft,
char **outbuf,
size_t outleft)
{
size_t i;
if (!inbuf)
return UNICONV_SUCCESS;
for (i = 0; i < inleft; i++) {
int seqlen = ucs4toutf8(**inbuf, NULL);
if (seqlen < 0)
return UNICONV_EILSEQ;
if (seqlen > outleft)
return UNICONV_E2BIG;
ucs4toutf8(**inbuf, *outbuf);
(*inbuf) += 1;
(*outbuf) += seqlen;
outleft -= seqlen;
}
return UNICONV_SUCCESS;
}
static int
utf8_decode(struct converter *conv,
const char **inbuf,
size_t inleft,
uc_char_t **outbuf,
size_t outleft)
{
while (inleft) {
uc_char_t unichar;
int seqlen = ucs4fromutf8(*inbuf, &unichar, inleft);
if (seqlen == -2)
return UNICONV_EINVAL;
else if (seqlen < 0)
return UNICONV_EILSEQ;
if (!outleft)
return UNICONV_E2BIG;
**outbuf = unichar;
(*outbuf) += 1;
outleft -= 1;
(*inbuf) += seqlen;
inleft -= seqlen;
}
return UNICONV_SUCCESS;
}
static int
utf16_encode(struct converter *conv,
const uc_char_t **inbuf,
size_t inleft,
char **outbuf,
size_t outleft)
{
size_t i;
uc_uint16_t **soutbuf = (uc_uint16_t **)outbuf;
if (!inbuf)
return UNICONV_SUCCESS;
for (i = 0; i < inleft; i++) {
int seqlen = ucs4toutf16(**inbuf, NULL);
if (seqlen < 0)
return UNICONV_EILSEQ;
if (seqlen * sizeof(uc_uint16_t) > outleft)
return UNICONV_E2BIG;
ucs4toutf16(**inbuf, *soutbuf);
(*inbuf) += 1;
(*soutbuf) += seqlen;
outleft -= seqlen;
}
return UNICONV_SUCCESS;
}
static int
utf16_decode(struct converter *conv,
const char **inbuf,
size_t inleft,
uc_char_t **outbuf,
size_t outleft)
{
const uc_uint16_t **sinbuf = (const uc_uint16_t**)inbuf;
while (inleft) {
uc_char_t unichar;
int seqlen = ucs4fromutf16(*sinbuf, &unichar,
inleft / sizeof(uc_uint16_t));
if (seqlen == -2)
return UNICONV_EINVAL;
else if (seqlen < 0)
return UNICONV_EILSEQ;
if (!outleft)
return UNICONV_E2BIG;
**outbuf = unichar;
(*outbuf) += 1;
outleft -= 1;
(*sinbuf) += seqlen;
inleft -= seqlen * sizeof(uc_uint16_t);
}
return UNICONV_SUCCESS;
}
static int
utf32_encode(struct converter *conv,
const uc_char_t **inbuf,
size_t inleft,
char **outbuf,
size_t outleft)
{
if (!inbuf)
return UNICONV_SUCCESS;
if (inleft * sizeof(uc_char_t) > outleft)
return UNICONV_E2BIG;
memcpy(*outbuf, *inbuf, inleft * sizeof(uc_char_t));
(*inbuf) += inleft;
(*outbuf) += inleft * sizeof(uc_char_t);
return UNICONV_SUCCESS;
}
static int
utf32_decode(struct converter *conv,
const char **inbuf,
size_t inleft,
uc_char_t **outbuf,
size_t outleft)
{
if (inleft & 3)
return UNICONV_EINVAL;
if (inleft > outleft)
return UNICONV_E2BIG;
memcpy(*outbuf, *inbuf, inleft);
(*inbuf) += inleft;
(*outbuf) += inleft / sizeof(uc_char_t);
return UNICONV_SUCCESS;
}
static void
utfconverter_close(struct converter *conv)
{
free(conv);
}
struct converter *
utfconverter_open(const char *charset)
{
struct utfconverter *conv;
if (strcmp(charset, "utf_8") && strcmp(charset, "utf_16") &&
strcmp(charset, "utf_32"))
return NULL;
conv = malloc(sizeof(struct utfconverter));
if (!conv)
return NULL;
if (!strcmp(charset, "utf_8")) {
conv->base.encode = utf8_encode;
conv->base.decode = utf8_decode;
} else if (!strcmp(charset, "utf_16")) {
conv->base.encode = utf16_encode;
conv->base.decode = utf16_decode;
} else if (!strcmp(charset, "utf_32")) {
conv->base.encode = utf32_encode;
conv->base.decode = utf32_decode;
}
conv->base.close = utfconverter_close;
conv->base.reset = NULL;
return &conv->base;
}
|