]>
git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Console/TerminalDxe/Vtutf8.c
9cf52d90b8b47883194311b9e471ebe547b76db8
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
)) {
35 GetOneValidUtf8Char (TerminalDevice
, &Utf8Char
, &ValidBytes
);
37 if (ValidBytes
< 1 || ValidBytes
> 3) {
41 Utf8ToUnicode (Utf8Char
, ValidBytes
, (CHAR16
*) &UnicodeChar
);
43 UnicodeFiFoInsertOneKey (TerminalDevice
, UnicodeChar
);
48 Get one valid VT-UTF8 characters set from Raw Data FIFO.
50 @param Utf8Device The terminal device.
51 @param Utf8Char Returned valid VT-UTF8 characters set.
52 @param ValidBytes The count of returned VT-VTF8 characters.
53 If ValidBytes is zero, no valid VT-UTF8 returned.
58 IN TERMINAL_DEV
*Utf8Device
,
59 OUT UTF8_CHAR
*Utf8Char
,
72 // if no valid Utf8 char is found in the RawFiFo,
73 // then *ValidBytes will be zero.
77 while (!IsRawFiFoEmpty (Utf8Device
)) {
79 RawFiFoRemoveOneKey (Utf8Device
, &Temp
);
81 switch (*ValidBytes
) {
84 if ((Temp
& 0x80) == 0) {
90 Utf8Char
->Utf8_1
= Temp
;
94 } else if ((Temp
& 0xe0) == 0xc0) {
100 Utf8Char
->Utf8_2
[1] = Temp
;
102 } else if ((Temp
& 0xf0) == 0xe0) {
104 // three-byte utf8 char
108 Utf8Char
->Utf8_3
[2] = Temp
;
114 // reset *ValidBytes to zero, let valid utf8 char search restart
123 // two-byte utf8 char go on
125 if ((Temp
& 0xc0) == 0x80) {
127 Utf8Char
->Utf8_2
[0] = Temp
;
139 // three-byte utf8 char go on
141 if ((Temp
& 0xc0) == 0x80) {
143 Utf8Char
->Utf8_3
[1] = Temp
;
146 Utf8Char
->Utf8_3
[0] = Temp
;
151 // reset *ValidBytes and Index to zero, let valid utf8 char search restart
171 Translate VT-UTF8 characters into one Unicode character.
174 Bits per Character | Unicode Character Range | Unicode Binary Encoding | UTF8 Binary Encoding
175 0-7 | 0x0000 - 0x007F | 00000000 0xxxxxxx | 0xxxxxxx
176 8-11 | 0x0080 - 0x07FF | 00000xxx xxxxxxxx | 110xxxxx 10xxxxxx
177 12-16 | 0x0800 - 0xFFFF | xxxxxxxx xxxxxxxx | 1110xxxx 10xxxxxx 10xxxxxx
180 @param Utf8Char VT-UTF8 character set needs translating.
181 @param ValidBytes The count of valid VT-UTF8 characters.
182 @param UnicodeChar Returned unicode character.
187 IN UTF8_CHAR Utf8Char
,
189 OUT CHAR16
*UnicodeChar
201 // translate utf8 code to unicode, in terminal standard,
202 // up to 3 bytes utf8 code is supported.
204 switch (ValidBytes
) {
207 // one-byte utf8 code
209 *UnicodeChar
= (UINT16
) Utf8Char
.Utf8_1
;
214 // two-byte utf8 code
216 Byte0
= Utf8Char
.Utf8_2
[0];
217 Byte1
= Utf8Char
.Utf8_2
[1];
219 UnicodeByte0
= (UINT8
) ((Byte1
<< 6) | (Byte0
& 0x3f));
220 UnicodeByte1
= (UINT8
) ((Byte1
>> 2) & 0x07);
221 *UnicodeChar
= (UINT16
) (UnicodeByte0
| (UnicodeByte1
<< 8));
226 // three-byte utf8 code
228 Byte0
= Utf8Char
.Utf8_3
[0];
229 Byte1
= Utf8Char
.Utf8_3
[1];
230 Byte2
= Utf8Char
.Utf8_3
[2];
232 UnicodeByte0
= (UINT8
) ((Byte1
<< 6) | (Byte0
& 0x3f));
233 UnicodeByte1
= (UINT8
) ((Byte2
<< 4) | ((Byte1
>> 2) & 0x0f));
234 *UnicodeChar
= (UINT16
) (UnicodeByte0
| (UnicodeByte1
<< 8));
244 Translate one Unicode character into VT-UTF8 characters.
247 Bits per Character | Unicode Character Range | Unicode Binary Encoding | UTF8 Binary Encoding
248 0-7 | 0x0000 - 0x007F | 00000000 0xxxxxxx | 0xxxxxxx
249 8-11 | 0x0080 - 0x07FF | 00000xxx xxxxxxxx | 110xxxxx 10xxxxxx
250 12-16 | 0x0800 - 0xFFFF | xxxxxxxx xxxxxxxx | 1110xxxx 10xxxxxx 10xxxxxx
253 @param Unicode Unicode character need translating.
254 @param Utf8Char Return VT-UTF8 character set.
255 @param ValidBytes The count of valid VT-UTF8 characters. If
256 ValidBytes is zero, no valid VT-UTF8 returned.
262 OUT UTF8_CHAR
*Utf8Char
,
263 OUT UINT8
*ValidBytes
269 // translate unicode to utf8 code
271 UnicodeByte0
= (UINT8
) Unicode
;
272 UnicodeByte1
= (UINT8
) (Unicode
>> 8);
274 if (Unicode
< 0x0080) {
276 Utf8Char
->Utf8_1
= (UINT8
) (UnicodeByte0
& 0x7f);
279 } else if (Unicode
< 0x0800) {
281 // byte sequence: high -> low
282 // Utf8_2[0], Utf8_2[1]
284 Utf8Char
->Utf8_2
[1] = (UINT8
) ((UnicodeByte0
& 0x3f) + 0x80);
285 Utf8Char
->Utf8_2
[0] = (UINT8
) ((((UnicodeByte1
<< 2) + (UnicodeByte0
>> 6)) & 0x1f) + 0xc0);
291 // byte sequence: high -> low
292 // Utf8_3[0], Utf8_3[1], Utf8_3[2]
294 Utf8Char
->Utf8_3
[2] = (UINT8
) ((UnicodeByte0
& 0x3f) + 0x80);
295 Utf8Char
->Utf8_3
[1] = (UINT8
) ((((UnicodeByte1
<< 2) + (UnicodeByte0
>> 6)) & 0x3f) + 0x80);
296 Utf8Char
->Utf8_3
[0] = (UINT8
) (((UnicodeByte1
>> 4) & 0x0f) + 0xe0);
304 Check if input string is valid VT-UTF8 string.
306 @param TerminalDevice The terminal device.
307 @param WString The input string.
309 @retval EFI_SUCCESS If all input characters are valid.
314 IN TERMINAL_DEV
*TerminalDevice
,
319 // to utf8, all kind of characters are supported.