summaryrefslogtreecommitdiffstats
path: root/src/core/utf8.c
blob: 4ee01baf9d94c8e86176b1b217b31ad9400845b5 (plain) (blame)
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
/*
 * Copyright (C) 2022 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * You can also choose to distribute this program under the terms of
 * the Unmodified Binary Distribution Licence (as given in the file
 * COPYING.UBDL), provided that you have satisfied its requirements.
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

#include <stdint.h>
#include <assert.h>
#include <ipxe/utf8.h>

/** @file
 *
 * UTF-8 Unicode encoding
 *
 */

/**
 * Accumulate Unicode character from UTF-8 byte sequence
 *
 * @v utf8		UTF-8 accumulator
 * @v byte		UTF-8 byte
 * @ret character	Unicode character, or 0 if incomplete
 */
unsigned int utf8_accumulate ( struct utf8_accumulator *utf8, uint8_t byte ) {
	static unsigned int min[] = {
		UTF8_MIN_TWO,
		UTF8_MIN_THREE,
		UTF8_MIN_FOUR,
	};
	unsigned int shift;
	unsigned int len;
	uint8_t tmp;

	/* Handle continuation bytes */
	if ( UTF8_IS_CONTINUATION ( byte ) ) {

		/* Fail if this is an unexpected continuation byte */
		if ( utf8->remaining == 0 ) {
			DBGC ( utf8, "UTF8 %p unexpected %02x\n", utf8, byte );
			return UTF8_INVALID;
		}

		/* Apply continuation byte */
		utf8->character <<= UTF8_CONTINUATION_BITS;
		utf8->character |= ( byte & UTF8_CONTINUATION_MASK );

		/* Return 0 if more continuation bytes are expected */
		if ( --utf8->remaining != 0 )
			return 0;

		/* Fail if sequence is illegal */
		if ( utf8->character < utf8->min ) {
			DBGC ( utf8, "UTF8 %p illegal %02x\n", utf8,
			       utf8->character );
			return UTF8_INVALID;
		}

		/* Sanity check */
		assert ( utf8->character != 0 );

		/* Return completed character */
		DBGC2 ( utf8, "UTF8 %p accumulated %02x\n",
			utf8, utf8->character );
		return utf8->character;
	}

	/* Reset state and report failure if this is an unexpected
	 * non-continuation byte.  Do not return UTF8_INVALID since
	 * doing so could cause us to drop a valid ASCII character.
	 */
	if ( utf8->remaining != 0 ) {
		shift = ( utf8->remaining * UTF8_CONTINUATION_BITS );
		DBGC ( utf8, "UTF8 %p unexpected %02x (partial %02x/%02x)\n",
		       utf8, byte, ( utf8->character << shift ),
		       ( ( 1 << shift ) - 1 ) );
		utf8->remaining = 0;
	}

	/* Handle initial bytes */
	if ( ! UTF8_IS_ASCII ( byte ) ) {

		/* Sanity check */
		assert ( utf8->remaining == 0 );

		/* Count total number of bytes in sequence */
		tmp = byte;
		len = 0;
		while ( tmp & UTF8_HIGH_BIT ) {
			tmp <<= 1;
			len++;
		}

		/* Check for illegal length */
		if ( len > UTF8_MAX_LEN ) {
			DBGC ( utf8, "UTF8 %p illegal %02x length %d\n",
			       utf8, byte, len );
			return UTF8_INVALID;
		}

		/* Store initial bits of character */
		utf8->character = ( tmp >> len );

		/* Store number of bytes remaining */
		len--;
		utf8->remaining = len;
		assert ( utf8->remaining > 0 );

		/* Store minimum legal value */
		utf8->min = min[ len - 1 ];
		assert ( utf8->min > 0 );

		/* Await continuation bytes */
		return 0;
	}

	/* Handle ASCII bytes */
	return byte;
}