aboutsummaryrefslogtreecommitdiff
path: root/lib/charset.c
blob: 8cd17ea1cb78363b17f049d05e4fb0f5153da37c (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
/*
 *  charset conversion utils
 *
 *  Copyright (c) 2017 Rob Clark
 *
 *  SPDX-License-Identifier:     GPL-2.0+
 */

#include <charset.h>
#include <malloc.h>

/*
 * utf8/utf16 conversion mostly lifted from grub
 */

size_t utf16_strlen(const uint16_t *in)
{
	size_t i;
	for (i = 0; in[i]; i++);
	return i;
}

size_t utf16_strnlen(const uint16_t *in, size_t count)
{
	size_t i;
	for (i = 0; count-- && in[i]; i++);
	return i;
}

uint16_t *utf16_strcpy(uint16_t *dest, const uint16_t *src)
{
	uint16_t *tmp = dest;

	while ((*dest++ = *src++) != '\0')
		/* nothing */;
	return tmp;

}

uint16_t *utf16_strdup(const uint16_t *s)
{
	uint16_t *new;
	if (!s || !(new = malloc((utf16_strlen(s) + 1) * 2)))
		return NULL;
	utf16_strcpy(new, s);
	return new;
}

/* Convert UTF-16 to UTF-8.  */
uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size)
{
	uint32_t code_high = 0;

	while (size--) {
		uint32_t code = *src++;

		if (code_high) {
			if (code >= 0xDC00 && code <= 0xDFFF) {
				/* Surrogate pair.  */
				code = ((code_high - 0xD800) << 10) + (code - 0xDC00) + 0x10000;

				*dest++ = (code >> 18) | 0xF0;
				*dest++ = ((code >> 12) & 0x3F) | 0x80;
				*dest++ = ((code >> 6) & 0x3F) | 0x80;
				*dest++ = (code & 0x3F) | 0x80;
			} else {
				/* Error...  */
				*dest++ = '?';
				/* *src may be valid. Don't eat it.  */
				src--;
			}

			code_high = 0;
		} else {
			if (code <= 0x007F) {
				*dest++ = code;
			} else if (code <= 0x07FF) {
				*dest++ = (code >> 6) | 0xC0;
				*dest++ = (code & 0x3F) | 0x80;
			} else if (code >= 0xD800 && code <= 0xDBFF) {
				code_high = code;
				continue;
			} else if (code >= 0xDC00 && code <= 0xDFFF) {
				/* Error... */
				*dest++ = '?';
			} else if (code < 0x10000) {
				*dest++ = (code >> 12) | 0xE0;
				*dest++ = ((code >> 6) & 0x3F) | 0x80;
				*dest++ = (code & 0x3F) | 0x80;
			} else {
				*dest++ = (code >> 18) | 0xF0;
				*dest++ = ((code >> 12) & 0x3F) | 0x80;
				*dest++ = ((code >> 6) & 0x3F) | 0x80;
				*dest++ = (code & 0x3F) | 0x80;
			}
		}
	}

	return dest;
}

uint16_t *utf8_to_utf16(uint16_t *dest, const uint8_t *src, size_t size)
{
	while (size--) {
		int extension_bytes;
		uint32_t code;

		extension_bytes = 0;
		if (*src <= 0x7f) {
			code = *src++;
			/* Exit on zero byte */
			if (!code)
				size = 0;
		} else if (*src <= 0xbf) {
			/* Illegal code */
			code = '?';
		} else if (*src <= 0xdf) {
			code = *src++ & 0x1f;
			extension_bytes = 1;
		} else if (*src <= 0xef) {
			code = *src++ & 0x0f;
			extension_bytes = 2;
		} else if (*src <= 0xf7) {
			code = *src++ & 0x07;
			extension_bytes = 3;
		} else {
			/* Illegal code */
			code = '?';
		}

		for (; extension_bytes && size; --size, --extension_bytes) {
			if ((*src & 0xc0) == 0x80) {
				code <<= 6;
				code |= *src++ & 0x3f;
			} else {
				/* Illegal code */
				code = '?';
				++src;
				--size;
				break;
			}
		}

		if (code < 0x10000) {
			*dest++ = code;
		} else {
			/*
			 * Simplified expression for
			 * (((code - 0x10000) >> 10) & 0x3ff) | 0xd800
			 */
			*dest++ = (code >> 10) + 0xd7c0;
			*dest++ = (code & 0x3ff) | 0xdc00;
		}
	}
	return dest;
}