]>
git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Console/TerminalDxe/Vtutf8.c
2 Implementation of translation upon VT-UTF8.
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
12 Translate all VT-UTF8 characters in the Raw FIFI into unicode characters,
13 and insert them into Unicode FIFO.
15 @param TerminalDevice The terminal device.
19 VTUTF8RawDataToUnicode (
20 IN TERMINAL_DEV
*TerminalDevice
29 // pop the raw data out from the raw fifo,
30 // and translate it into unicode, then push
31 // the unicode into unicode fifo, until the raw fifo is empty.
33 while (!IsRawFiFoEmpty (TerminalDevice
) && !IsUnicodeFiFoFull (TerminalDevice
)) {
34 GetOneValidUtf8Char (TerminalDevice
, &Utf8Char
, &ValidBytes
);
36 if ((ValidBytes
< 1) || (ValidBytes
> 3)) {
40 Utf8ToUnicode (Utf8Char
, ValidBytes
, (CHAR16
*)&UnicodeChar
);
42 UnicodeFiFoInsertOneKey (TerminalDevice
, UnicodeChar
);
47 Get one valid VT-UTF8 characters set from Raw Data FIFO.
49 @param Utf8Device The terminal device.
50 @param Utf8Char Returned valid VT-UTF8 characters set.
51 @param ValidBytes The count of returned VT-VTF8 characters.
52 If ValidBytes is zero, no valid VT-UTF8 returned.
57 IN TERMINAL_DEV
*Utf8Device
,
58 OUT UTF8_CHAR
*Utf8Char
,
71 // if no valid Utf8 char is found in the RawFiFo,
72 // then *ValidBytes will be zero.
76 while (!IsRawFiFoEmpty (Utf8Device
)) {
77 RawFiFoRemoveOneKey (Utf8Device
, &Temp
);
79 switch (*ValidBytes
) {
81 if ((Temp
& 0x80) == 0) {
87 Utf8Char
->Utf8_1
= Temp
;
90 } else if ((Temp
& 0xe0) == 0xc0) {
96 Utf8Char
->Utf8_2
[1] = Temp
;
97 } else if ((Temp
& 0xf0) == 0xe0) {
99 // three-byte utf8 char
103 Utf8Char
->Utf8_3
[2] = Temp
;
108 // reset *ValidBytes to zero, let valid utf8 char search restart
117 // two-byte utf8 char go on
119 if ((Temp
& 0xc0) == 0x80) {
120 Utf8Char
->Utf8_2
[0] = Temp
;
131 // three-byte utf8 char go on
133 if ((Temp
& 0xc0) == 0x80) {
135 Utf8Char
->Utf8_3
[1] = Temp
;
138 Utf8Char
->Utf8_3
[0] = Temp
;
143 // reset *ValidBytes and Index to zero, let valid utf8 char search restart
164 Translate VT-UTF8 characters into one Unicode character.
167 Bits per Character | Unicode Character Range | Unicode Binary Encoding | UTF8 Binary Encoding
168 0-7 | 0x0000 - 0x007F | 00000000 0xxxxxxx | 0xxxxxxx
169 8-11 | 0x0080 - 0x07FF | 00000xxx xxxxxxxx | 110xxxxx 10xxxxxx
170 12-16 | 0x0800 - 0xFFFF | xxxxxxxx xxxxxxxx | 1110xxxx 10xxxxxx 10xxxxxx
173 @param Utf8Char VT-UTF8 character set needs translating.
174 @param ValidBytes The count of valid VT-UTF8 characters.
175 @param UnicodeChar Returned unicode character.
180 IN UTF8_CHAR Utf8Char
,
182 OUT CHAR16
*UnicodeChar
194 // translate utf8 code to unicode, in terminal standard,
195 // up to 3 bytes utf8 code is supported.
197 switch (ValidBytes
) {
200 // one-byte utf8 code
202 *UnicodeChar
= (UINT16
)Utf8Char
.Utf8_1
;
207 // two-byte utf8 code
209 Byte0
= Utf8Char
.Utf8_2
[0];
210 Byte1
= Utf8Char
.Utf8_2
[1];
212 UnicodeByte0
= (UINT8
)((Byte1
<< 6) | (Byte0
& 0x3f));
213 UnicodeByte1
= (UINT8
)((Byte1
>> 2) & 0x07);
214 *UnicodeChar
= (UINT16
)(UnicodeByte0
| (UnicodeByte1
<< 8));
219 // three-byte utf8 code
221 Byte0
= Utf8Char
.Utf8_3
[0];
222 Byte1
= Utf8Char
.Utf8_3
[1];
223 Byte2
= Utf8Char
.Utf8_3
[2];
225 UnicodeByte0
= (UINT8
)((Byte1
<< 6) | (Byte0
& 0x3f));
226 UnicodeByte1
= (UINT8
)((Byte2
<< 4) | ((Byte1
>> 2) & 0x0f));
227 *UnicodeChar
= (UINT16
)(UnicodeByte0
| (UnicodeByte1
<< 8));
237 Translate one Unicode character into VT-UTF8 characters.
240 Bits per Character | Unicode Character Range | Unicode Binary Encoding | UTF8 Binary Encoding
241 0-7 | 0x0000 - 0x007F | 00000000 0xxxxxxx | 0xxxxxxx
242 8-11 | 0x0080 - 0x07FF | 00000xxx xxxxxxxx | 110xxxxx 10xxxxxx
243 12-16 | 0x0800 - 0xFFFF | xxxxxxxx xxxxxxxx | 1110xxxx 10xxxxxx 10xxxxxx
246 @param Unicode Unicode character need translating.
247 @param Utf8Char Return VT-UTF8 character set.
248 @param ValidBytes The count of valid VT-UTF8 characters. If
249 ValidBytes is zero, no valid VT-UTF8 returned.
255 OUT UTF8_CHAR
*Utf8Char
,
256 OUT UINT8
*ValidBytes
263 // translate unicode to utf8 code
265 UnicodeByte0
= (UINT8
)Unicode
;
266 UnicodeByte1
= (UINT8
)(Unicode
>> 8);
268 if (Unicode
< 0x0080) {
269 Utf8Char
->Utf8_1
= (UINT8
)(UnicodeByte0
& 0x7f);
271 } else if (Unicode
< 0x0800) {
273 // byte sequence: high -> low
274 // Utf8_2[0], Utf8_2[1]
276 Utf8Char
->Utf8_2
[1] = (UINT8
)((UnicodeByte0
& 0x3f) + 0x80);
277 Utf8Char
->Utf8_2
[0] = (UINT8
)((((UnicodeByte1
<< 2) + (UnicodeByte0
>> 6)) & 0x1f) + 0xc0);
282 // byte sequence: high -> low
283 // Utf8_3[0], Utf8_3[1], Utf8_3[2]
285 Utf8Char
->Utf8_3
[2] = (UINT8
)((UnicodeByte0
& 0x3f) + 0x80);
286 Utf8Char
->Utf8_3
[1] = (UINT8
)((((UnicodeByte1
<< 2) + (UnicodeByte0
>> 6)) & 0x3f) + 0x80);
287 Utf8Char
->Utf8_3
[0] = (UINT8
)(((UnicodeByte1
>> 4) & 0x0f) + 0xe0);
294 Check if input string is valid VT-UTF8 string.
296 @param TerminalDevice The terminal device.
297 @param WString The input string.
299 @retval EFI_SUCCESS If all input characters are valid.
304 IN TERMINAL_DEV
*TerminalDevice
,
309 // to utf8, all kind of characters are supported.