]>
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, Intel Corporation. <BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Translate all VT-UTF8 characters in the Raw FIFI into unicode characters,
19 and insert them into Unicode FIFO.
21 @param TerminalDevice The terminal device.
27 VTUTF8RawDataToUnicode (
28 IN TERMINAL_DEV
*TerminalDevice
37 // pop the raw data out from the raw fifo,
38 // and translate it into unicode, then push
39 // the unicode into unicode fifo, until the raw fifo is empty.
41 while (!IsRawFiFoEmpty (TerminalDevice
)) {
43 GetOneValidUtf8Char (TerminalDevice
, &Utf8Char
, &ValidBytes
);
45 if (ValidBytes
< 1 || ValidBytes
> 3) {
49 Utf8ToUnicode (Utf8Char
, ValidBytes
, (CHAR16
*) &UnicodeChar
);
51 UnicodeFiFoInsertOneKey (TerminalDevice
, UnicodeChar
);
56 Get one valid VT-UTF8 characters set from Raw Data FIFO.
58 @param Utf8Device The terminal device.
59 @param Utf8Char Returned valid VT-UTF8 characters set.
60 @param ValidBytes The count of returned VT-VTF8 characters.
61 If ValidBytes is zero, no valid VT-UTF8 returned.
66 IN TERMINAL_DEV
*Utf8Device
,
67 OUT UTF8_CHAR
*Utf8Char
,
80 // if no valid Utf8 char is found in the RawFiFo,
81 // then *ValidBytes will be zero.
85 while (!IsRawFiFoEmpty (Utf8Device
)) {
87 RawFiFoRemoveOneKey (Utf8Device
, &Temp
);
89 switch (*ValidBytes
) {
92 if ((Temp
& 0x80) == 0) {
98 Utf8Char
->Utf8_1
= Temp
;
102 } else if ((Temp
& 0xe0) == 0xc0) {
104 // two-byte utf8 char
108 Utf8Char
->Utf8_2
[1] = Temp
;
110 } else if ((Temp
& 0xf0) == 0xe0) {
112 // three-byte utf8 char
116 Utf8Char
->Utf8_3
[2] = Temp
;
122 // reset *ValidBytes to zero, let valid utf8 char search restart
130 if ((Temp
& 0xc0) == 0x80) {
132 Utf8Char
->Utf8_2
[0] = Temp
;
143 if ((Temp
& 0xc0) == 0x80) {
145 Utf8Char
->Utf8_3
[2 - Index
] = Temp
;
170 Translate VT-UTF8 characters into one Unicode character.
173 Bits per Character | Unicode Character Range | Unicode Binary Encoding | UTF8 Binary Encoding
174 0-7 | 0x0000 - 0x007F | 00000000 0xxxxxxx | 0xxxxxxx
175 8-11 | 0x0080 - 0x07FF | 00000xxx xxxxxxxx | 110xxxxx 10xxxxxx
176 12-16 | 0x0800 - 0xFFFF | xxxxxxxx xxxxxxxx | 1110xxxx 10xxxxxx 10xxxxxx
179 @param Utf8Char VT-UTF8 character set needs translating.
180 @param ValidBytes The count of valid VT-UTF8 characters.
181 @param UnicodeChar Returned unicode character.
188 IN UTF8_CHAR Utf8Char
,
190 OUT CHAR16
*UnicodeChar
202 // translate utf8 code to unicode, in terminal standard,
203 // up to 3 bytes utf8 code is supported.
205 switch (ValidBytes
) {
208 // one-byte utf8 code
210 *UnicodeChar
= (UINT16
) Utf8Char
.Utf8_1
;
215 // two-byte utf8 code
217 Byte0
= Utf8Char
.Utf8_2
[0];
218 Byte1
= Utf8Char
.Utf8_2
[1];
220 UnicodeByte0
= (UINT8
) ((Byte1
<< 6) | (Byte0
& 0x3f));
221 UnicodeByte1
= (UINT8
) ((Byte1
>> 2) & 0x07);
222 *UnicodeChar
= (UINT16
) (UnicodeByte0
| (UnicodeByte1
<< 8));
227 // three-byte utf8 code
229 Byte0
= Utf8Char
.Utf8_3
[0];
230 Byte1
= Utf8Char
.Utf8_3
[1];
231 Byte2
= Utf8Char
.Utf8_3
[2];
233 UnicodeByte0
= (UINT8
) ((Byte1
<< 6) | (Byte0
& 0x3f));
234 UnicodeByte1
= (UINT8
) ((Byte2
<< 4) | ((Byte1
>> 2) & 0x0f));
235 *UnicodeChar
= (UINT16
) (UnicodeByte0
| (UnicodeByte1
<< 8));
245 Translate one Unicode character into VT-UTF8 characters.
248 Bits per Character | Unicode Character Range | Unicode Binary Encoding | UTF8 Binary Encoding
249 0-7 | 0x0000 - 0x007F | 00000000 0xxxxxxx | 0xxxxxxx
250 8-11 | 0x0080 - 0x07FF | 00000xxx xxxxxxxx | 110xxxxx 10xxxxxx
251 12-16 | 0x0800 - 0xFFFF | xxxxxxxx xxxxxxxx | 1110xxxx 10xxxxxx 10xxxxxx
254 @param Unicode Unicode character need translating.
255 @param Utf8Char Return VT-UTF8 character set.
256 @param ValidBytes The count of valid VT-UTF8 characters. If
257 ValidBytes is zero, no valid VT-UTF8 returned.
265 OUT UTF8_CHAR
*Utf8Char
,
266 OUT UINT8
*ValidBytes
272 // translate unicode to utf8 code
274 UnicodeByte0
= (UINT8
) Unicode
;
275 UnicodeByte1
= (UINT8
) (Unicode
>> 8);
277 if (Unicode
< 0x0080) {
279 Utf8Char
->Utf8_1
= (UINT8
) (UnicodeByte0
& 0x7f);
282 } else if (Unicode
< 0x0800) {
284 // byte sequence: high -> low
285 // Utf8_2[0], Utf8_2[1]
287 Utf8Char
->Utf8_2
[1] = (UINT8
) ((UnicodeByte0
& 0x3f) + 0x80);
288 Utf8Char
->Utf8_2
[0] = (UINT8
) ((((UnicodeByte1
<< 2) + (UnicodeByte0
>> 6)) & 0x1f) + 0xc0);
294 // byte sequence: high -> low
295 // Utf8_3[0], Utf8_3[1], Utf8_3[2]
297 Utf8Char
->Utf8_3
[2] = (UINT8
) ((UnicodeByte0
& 0x3f) + 0x80);
298 Utf8Char
->Utf8_3
[1] = (UINT8
) ((((UnicodeByte1
<< 2) + (UnicodeByte0
>> 6)) & 0x3f) + 0x80);
299 Utf8Char
->Utf8_3
[0] = (UINT8
) (((UnicodeByte1
>> 4) & 0x0f) + 0xe0);
307 Check if input string is valid VT-UTF8 string.
309 @param TerminalDevice The terminal device.
310 @param WString The input string.
312 @retval EFI_SUCCESS If all input characters are valid.
317 IN TERMINAL_DEV
*TerminalDevice
,
322 // to utf8, all kind of characters are supported.