summaryrefslogtreecommitdiffstats
blob: 5df8769937821a46fcbdfaa00938d5c42a1546c9 (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
/*
 * Copyright (c) 2015 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#pragma once

#include <cstddef>

namespace Swift {

// UTF-8 validation based on the description in https://tools.ietf.org/html/rfc3629#section-3 .
template <typename CharType>
bool UTF8IsValid(const CharType* data, size_t length) {
	bool isValid = true;
	const CharType* current = data;
	const CharType* end = data + length;
	while (isValid && (current < end)) {
		// one byte sequences
		if ((*current & 0x80) == 0x0) {
			current++;
			continue;
		}
		// longer byte sequences
		else {
			// two byte sequences
			if ((*current & 0xE0) == 0xC0) {
				current++;
				if ( (current < end) && ((*current & 0xC0) == 0x80) ) {
					current++;
					continue;
				}
			}
			// three byte sequences
			else if ((*current & 0xF0) == 0xE0) {
				current++;
				if ( ((current + 1) < end) && ((*current & 0xC0) == 0x80) ) {
					current++;
					if ((*current & 0xC0) == 0x80) {
						current++;
						continue;
					}
				}
			}
			// four byte sequences
			else if ((*current & 0xF8) == 0xF0) {
				current++;
				if ( ((current + 2) < end) && ((*current & 0xC0) == 0x80) ) {
					current++;
					if ((*current & 0xC0) == 0x80) {
						current++;
						if ((*current & 0xC0) == 0x80) {
							current++;
							continue;
						}
					}
				}
			}
			// invalid sequences
			isValid = false;
		}
	}
	return isValid;
}

}