summaryrefslogtreecommitdiff
path: root/cras/src/server/cras_utf8.c
blob: ebb8a89aeb65c15f6ee6f4c265e91f1576cbd393 (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
/* Copyright 2016 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>

#ifdef CRAS_DBUS
#include <dbus/dbus.h>
#endif

#include "cras_utf8.h"
#include "cras_util.h"

static const uint8_t kUTF8ByteOrderMask[3] = { 0xef, 0xbb, 0xbf };

typedef struct u8range {
	uint8_t min;
	uint8_t max;
} u8range_t;

static const u8range_t kUTF8TwoByteSeq[] = {
	{ 0xc2, 0xdf },
	{ 0x80, 0xbf },
	{ 0, 0 },
};

static const u8range_t kUTF8ByteSeqE0[] = {
	{ 0xe0, 0xe0 },
	{ 0xa0, 0xbf },
	{ 0x80, 0xbf },
	{ 0, 0 },
};

static const u8range_t kUTF8ByteSeqE1EC[] = {
	{ 0xe1, 0xec },
	{ 0x80, 0xbf },
	{ 0x80, 0xbf },
	{ 0, 0 },
};

static const u8range_t kUTF8ByteSeqED[] = {
	{ 0xed, 0xed },
	{ 0x80, 0x9f },
	{ 0x80, 0xbf },
	{ 0, 0 },
};

static const u8range_t kUTF8ByteSeqEEEF[] = {
	{ 0xee, 0xef },
	{ 0x80, 0xbf },
	{ 0x80, 0xbf },
	{ 0, 0 },
};

static const u8range_t kUTF8ByteSeqF0[] = {
	{ 0xf0, 0xf0 }, { 0x90, 0xbf }, { 0x80, 0xbf },
	{ 0x80, 0xbf }, { 0, 0 },
};

static const u8range_t kUTF8ByteSeqF1F3[] = {
	{ 0xf1, 0xf3 }, { 0x80, 0xbf }, { 0x80, 0xbf },
	{ 0x80, 0xbf }, { 0, 0 },
};

static const u8range_t kUTF8ByteSeqF4[] = {
	{ 0xf4, 0xf4 }, { 0x80, 0x8f }, { 0x80, 0xbf },
	{ 0x80, 0xbf }, { 0, 0 },
};

static const u8range_t kUTF8NullRange[] = { { 0, 0 } };

typedef struct utf8seq {
	const u8range_t *ranges;
} utf8seq_t;

static const utf8seq_t kUTF8Sequences[] = {
	{ kUTF8TwoByteSeq },  { kUTF8ByteSeqE0 },   { kUTF8ByteSeqE1EC },
	{ kUTF8ByteSeqED },   { kUTF8ByteSeqEEEF }, { kUTF8ByteSeqF0 },
	{ kUTF8ByteSeqF1F3 }, { kUTF8ByteSeqF4 },   { kUTF8NullRange }
};

int valid_utf8_string(const char *string, size_t *bad_pos)
{
	int bom_chars = 0;
	uint8_t byte;
	const char *pos = string;
	int ret = 1;
	const utf8seq_t *seq = NULL;
	const u8range_t *range = NULL;

	if (!pos) {
		ret = 0;
		goto error;
	}

	while ((byte = (uint8_t) * (pos++))) {
		if (!range || range->min == 0) {
			if (byte < 128) {
				/* Ascii character. */
				continue;
			}

			if (bom_chars < ARRAY_SIZE(kUTF8ByteOrderMask)) {
				if (byte == kUTF8ByteOrderMask[bom_chars]) {
					bom_chars++;
					continue;
				} else {
					/* Characters not matching BOM.
					 * Rewind and assume that there is
					 * no BOM. */
					bom_chars =
						ARRAY_SIZE(kUTF8ByteOrderMask);
					pos = string;
					continue;
				}
			}

			/* Find the matching sequence of characters by
			 * matching the first character in the sequence.
			 */
			seq = kUTF8Sequences;
			while (seq->ranges->min != 0) {
				if (byte >= seq->ranges->min &&
				    byte <= seq->ranges->max) {
					/* Matching sequence. */
					break;
				}
				seq++;
			}

			if (seq->ranges->min == 0) {
				/* Could not find a matching sequence. */
				ret = 0;
				goto error;
			}

			/* Found the appropriate sequence. */
			range = seq->ranges + 1;
			continue;
		}

		if (byte >= range->min && byte <= range->max) {
			range++;
			continue;
		}

		/* This character doesn't belong in UTF8. */
		ret = 0;
		goto error;
	}

	if (range && range->min != 0) {
		/* Stopped in the middle of a sequence. */
		ret = 0;
	}

error:
	if (bad_pos)
		*bad_pos = pos - string - 1;
	return ret;
}

#ifdef CRAS_DBUS
/* Use the DBus implementation if available to ensure that the UTF-8
 * sequences match those expected by the DBus implementation. */

int is_utf8_string(const char *string)
{
	return !!dbus_validate_utf8(string, NULL);
}

#else

int is_utf8_string(const char *string)
{
	return valid_utf8_string(string, NULL);
}

#endif