summaryrefslogtreecommitdiff
path: root/utfconverter.c
blob: 2c57d3c701266e90e642a7e6e4d26703dd1e026c (plain)
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include "converter.h"
#include "utfconverter.h"
#include "uniconv.h"
#include "unicode.h"

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

#define FLAG_USE_BOM_ENDIAN  (1 << 0)
#define FLAG_DONE_BOM_ENDIAN (1 << 1)
struct utfconverter {
    struct converter base;

    unsigned int flags;
    int little_endian;
};

static int
is_big_endian(void)
{
    static const union {
	int iv;
	char cv[4];
    } u = { 0x12345678 };

    return u.cv[0] == 0x12;
}

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 ((size_t)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)
{
    struct utfconverter *uc = (struct utfconverter*)conv;
    size_t i;

    if (!inbuf)
	return UNICONV_SUCCESS;

    for (i = 0; i < inleft; i++) {
	int seqlen = ucs4toutf16(**inbuf, NULL);
	uc_uint16_t utf16[2];
	if (seqlen < 0)
	    return UNICONV_EILSEQ;
	if ((size_t)seqlen * 2 > outleft)
	    return UNICONV_E2BIG;

	ucs4toutf16(**inbuf, utf16);
	if (uc->little_endian) {
	    (*outbuf)[0] = utf16[0] & 0xff;
	    (*outbuf)[1] = utf16[0] >> 8;
	    if (seqlen == 2) {
		(*outbuf)[2] = utf16[1] & 0xff;
		(*outbuf)[3] = utf16[1] >> 8;
	    }
	} else {
	    (*outbuf)[0] = utf16[0] >> 8;
	    (*outbuf)[1] = utf16[0] & 0xff;
	    if (seqlen == 2) {
		(*outbuf)[2] = utf16[1] >> 8;
		(*outbuf)[3] = utf16[1] & 0xff;
	    }
	}
	(*inbuf) += 1;
	(*outbuf) += seqlen * 2;
	outleft -= seqlen * 2;
    }

    return UNICONV_SUCCESS;
}

static int
utf16_decode(struct converter *conv,
	     const char **sinbuf,
	     size_t inleft,
	     uc_char_t **outbuf,
	     size_t outleft)
{
    struct utfconverter *uc = (struct utfconverter*)conv;
    const uc_uint8_t **inbuf = (const uc_uint8_t **)sinbuf;

    while (inleft) {
	uc_char_t unichar;
	uc_uint16_t utf16[2];
	int seqlen;

	if (inleft < 2)
	    return UNICONV_EINVAL;

	if (uc->little_endian)
	    utf16[0] = (*inbuf)[1] << 8 | (*inbuf)[0];
	else
	    utf16[0] = (*inbuf)[0] << 8 | (*inbuf)[1];
	if (utf16[0] >= 0xd800 && utf16[0] <= 0xbeff)
	    seqlen = 2;
	else
	    seqlen = 1;
	if (inleft < (size_t)seqlen * 2)
	    return UNICONV_EINVAL;
	if (seqlen == 2) {
	    if (uc->little_endian)
		utf16[1] = (*inbuf)[3] << 8 | (*inbuf)[2];
	    else
		utf16[1] = (*inbuf)[2] << 8 | (*inbuf)[3];
	}
	seqlen = ucs4fromutf16(utf16, &unichar, seqlen);
	if (seqlen == -2)
	    return UNICONV_EINVAL;
	else if (seqlen < 0)
	    return UNICONV_EILSEQ;
	if (!outleft)
	    return UNICONV_E2BIG;

	/* BOM */
	if (unichar == 0xfffe && uc->flags & FLAG_USE_BOM_ENDIAN &&
	    !(uc->flags & FLAG_DONE_BOM_ENDIAN)) {
	    uc->flags &= ~FLAG_DONE_BOM_ENDIAN;
	    uc->little_endian ^= 1;
	    unichar = 0xfeff;
	}
	**outbuf = unichar;
	(*outbuf) += 1;
	outleft -= 1;

	(*inbuf) += seqlen * 2;
	inleft -= seqlen * 2;
    }

    return UNICONV_SUCCESS;
}

static int
utf32_encode(struct converter *conv,
	     const uc_char_t **inbuf,
	     size_t inleft,
	     char **outbuf,
	     size_t outleft)
{
    struct utfconverter *uc = (struct utfconverter*)conv;

    if (!inbuf)
	return UNICONV_SUCCESS;

    while (inleft) {
	if (outleft < 4)
	    return UNICONV_E2BIG;

	if (uc->little_endian) {
	    (*outbuf)[0] = ((**inbuf) & 0x000000ff) >>  0;
	    (*outbuf)[1] = ((**inbuf) & 0x0000ff00) >>  8;
	    (*outbuf)[2] = ((**inbuf) & 0x00ff0000) >> 16;
	    (*outbuf)[3] = ((**inbuf) & 0xff000000) >> 24;
	} else {
	    (*outbuf)[3] = ((**inbuf) & 0x000000ff) >>  0;
	    (*outbuf)[2] = ((**inbuf) & 0x0000ff00) >>  8;
	    (*outbuf)[1] = ((**inbuf) & 0x00ff0000) >> 16;
	    (*outbuf)[0] = ((**inbuf) & 0xff000000) >> 24;
	}
	(*inbuf) += 1;
	(*outbuf) += 4;
	inleft -= 1;
	outleft -= 4;
    }

    return UNICONV_SUCCESS;
}

static int
utf32_decode(struct converter *conv,
	     const char **sinbuf,
	     size_t inleft,
	     uc_char_t **outbuf,
	     size_t outleft)
{
    struct utfconverter *uc = (struct utfconverter*)conv;
    const uc_uint8_t **inbuf = (const uc_uint8_t **)sinbuf;

    if (inleft & 3)
	return UNICONV_EINVAL;

    while (inleft) {
	if (!outleft)
	    return UNICONV_E2BIG;

	if (uc->little_endian)
	    **outbuf =
		((*inbuf)[0] <<  0) |
		((*inbuf)[1] <<  8) |
		((*inbuf)[2] << 16) |
		((*inbuf)[3] << 24);
	else
	    **outbuf =
		((*inbuf)[3] <<  0) |
		((*inbuf)[2] <<  8) |
		((*inbuf)[1] << 16) |
		((*inbuf)[0] << 24);

	/* BOM */
	if (**outbuf == 0xfffe && uc->flags & FLAG_USE_BOM_ENDIAN &&
	    !(uc->flags & FLAG_DONE_BOM_ENDIAN)) {
	    uc->flags &= ~FLAG_DONE_BOM_ENDIAN;
	    uc->little_endian ^= 1;
	    **outbuf = 0xfeff;
	}

	(*inbuf) += 4;
	(*outbuf) += 1;
	inleft -= 4;
	outleft -= 1;
    }

    return UNICONV_SUCCESS;
}

static void
utfconverter_close(struct converter *conv)
{
    free(conv);
}

static void
utfconverter_reset(struct converter *suc)
{
    struct utfconverter *uc = (struct utfconverter*)suc;

    /* default to host endian, should be big endian? */
    if (uc->flags & FLAG_USE_BOM_ENDIAN)
	uc->little_endian = !is_big_endian();
    uc->flags &= ~FLAG_DONE_BOM_ENDIAN;
}

struct converter *
utfconverter_open(const char *charset)
{
    struct utfconverter *conv;

    if (strcmp(charset, "utf_8") &&
	strcmp(charset, "utf_16") &&
	strcmp(charset, "utf_16_le") &&
	strcmp(charset, "utf_16_be") &&
	strcmp(charset, "utf_32") &&
	strcmp(charset, "utf_32_le") &&
	strcmp(charset, "utf_32_be"))
	return NULL;

    conv = malloc(sizeof(struct utfconverter));
    if (!conv)
	return NULL;
    conv->flags = 0;
    conv->little_endian = !is_big_endian();
    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;
	conv->flags |= FLAG_USE_BOM_ENDIAN;
    } else if (!strcmp(charset, "utf_16_le")) {
	conv->base.encode = utf16_encode;
	conv->base.decode = utf16_decode;
	conv->little_endian = 1;
    } else if (!strcmp(charset, "utf_16_be")) {
	conv->base.encode = utf16_encode;
	conv->base.decode = utf16_decode;
	conv->little_endian = 0;
    } else if (!strcmp(charset, "utf_32")) {
	conv->base.encode = utf32_encode;
	conv->base.decode = utf32_decode;
	conv->flags |= FLAG_USE_BOM_ENDIAN;
    } else if (!strcmp(charset, "utf_32_le")) {
	conv->base.encode = utf32_encode;
	conv->base.decode = utf32_decode;
	conv->little_endian = 1;
    } else if (!strcmp(charset, "utf_32_be")) {
	conv->base.encode = utf32_encode;
	conv->base.decode = utf32_decode;
	conv->little_endian = 0;
    }
    conv->base.close = utfconverter_close;
    conv->base.reset = utfconverter_reset;

    return &conv->base;
}