]>
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.
25 VTUTF8RawDataToUnicode (
26 IN TERMINAL_DEV
*TerminalDevice
35 // pop the raw data out from the raw fifo,
36 // and translate it into unicode, then push
37 // the unicode into unicode fifo, until the raw fifo is empty.
39 while (!IsRawFiFoEmpty (TerminalDevice
)) {
41 GetOneValidUtf8Char (TerminalDevice
, &Utf8Char
, &ValidBytes
);
43 if (ValidBytes
< 1 || ValidBytes
> 3) {
47 Utf8ToUnicode (Utf8Char
, ValidBytes
, (CHAR16
*) &UnicodeChar
);
49 UnicodeFiFoInsertOneKey (TerminalDevice
, UnicodeChar
);
54 Get one valid VT-UTF8 characters set from Raw Data FIFO.
56 @param Utf8Device The terminal device.
57 @param Utf8Char Returned valid VT-UTF8 characters set.
58 @param ValidBytes The count of returned VT-VTF8 characters.
59 If ValidBytes is zero, no valid VT-UTF8 returned.
64 IN TERMINAL_DEV
*Utf8Device
,
65 OUT UTF8_CHAR
*Utf8Char
,
78 // if no valid Utf8 char is found in the RawFiFo,
79 // then *ValidBytes will be zero.
83 while (!IsRawFiFoEmpty (Utf8Device
)) {
85 RawFiFoRemoveOneKey (Utf8Device
, &Temp
);
87 switch (*ValidBytes
) {
90 if ((Temp
& 0x80) == 0) {
96 Utf8Char
->Utf8_1
= Temp
;
100 } else if ((Temp
& 0xe0) == 0xc0) {
102 // two-byte utf8 char
106 Utf8Char
->Utf8_2
[1] = Temp
;
108 } else if ((Temp
& 0xf0) == 0xe0) {
110 // three-byte utf8 char
114 Utf8Char
->Utf8_3
[2] = Temp
;
120 // reset *ValidBytes to zero, let valid utf8 char search restart
128 if ((Temp
& 0xc0) == 0x80) {
130 Utf8Char
->Utf8_2
[0] = Temp
;
141 if ((Temp
& 0xc0) == 0x80) {
143 Utf8Char
->Utf8_3
[2 - Index
] = Temp
;
168 Translate VT-UTF8 characters into one Unicode character.
171 Bits per Character | Unicode Character Range | Unicode Binary Encoding | UTF8 Binary Encoding
172 0-7 | 0x0000 - 0x007F | 00000000 0xxxxxxx | 0xxxxxxx
173 8-11 | 0x0080 - 0x07FF | 00000xxx xxxxxxxx | 110xxxxx 10xxxxxx
174 12-16 | 0x0800 - 0xFFFF | xxxxxxxx xxxxxxxx | 1110xxxx 10xxxxxx 10xxxxxx
177 @param Utf8Char VT-UTF8 character set needs translating.
178 @param ValidBytes The count of valid VT-UTF8 characters.
179 @param UnicodeChar Returned unicode character.
184 IN UTF8_CHAR Utf8Char
,
186 OUT CHAR16
*UnicodeChar
198 // translate utf8 code to unicode, in terminal standard,
199 // up to 3 bytes utf8 code is supported.
201 switch (ValidBytes
) {
204 // one-byte utf8 code
206 *UnicodeChar
= (UINT16
) Utf8Char
.Utf8_1
;
211 // two-byte utf8 code
213 Byte0
= Utf8Char
.Utf8_2
[0];
214 Byte1
= Utf8Char
.Utf8_2
[1];
216 UnicodeByte0
= (UINT8
) ((Byte1
<< 6) | (Byte0
& 0x3f));
217 UnicodeByte1
= (UINT8
) ((Byte1
>> 2) & 0x07);
218 *UnicodeChar
= (UINT16
) (UnicodeByte0
| (UnicodeByte1
<< 8));
223 // three-byte utf8 code
225 Byte0
= Utf8Char
.Utf8_3
[0];
226 Byte1
= Utf8Char
.Utf8_3
[1];
227 Byte2
= Utf8Char
.Utf8_3
[2];
229 UnicodeByte0
= (UINT8
) ((Byte1
<< 6) | (Byte0
& 0x3f));
230 UnicodeByte1
= (UINT8
) ((Byte2
<< 4) | ((Byte1
>> 2) & 0x0f));
231 *UnicodeChar
= (UINT16
) (UnicodeByte0
| (UnicodeByte1
<< 8));
241 Translate one Unicode character into VT-UTF8 characters.
244 Bits per Character | Unicode Character Range | Unicode Binary Encoding | UTF8 Binary Encoding
245 0-7 | 0x0000 - 0x007F | 00000000 0xxxxxxx | 0xxxxxxx
246 8-11 | 0x0080 - 0x07FF | 00000xxx xxxxxxxx | 110xxxxx 10xxxxxx
247 12-16 | 0x0800 - 0xFFFF | xxxxxxxx xxxxxxxx | 1110xxxx 10xxxxxx 10xxxxxx
250 @param Unicode Unicode character need translating.
251 @param Utf8Char Return VT-UTF8 character set.
252 @param ValidBytes The count of valid VT-UTF8 characters. If
253 ValidBytes is zero, no valid VT-UTF8 returned.
259 OUT UTF8_CHAR
*Utf8Char
,
260 OUT UINT8
*ValidBytes
266 // translate unicode to utf8 code
268 UnicodeByte0
= (UINT8
) Unicode
;
269 UnicodeByte1
= (UINT8
) (Unicode
>> 8);
271 if (Unicode
< 0x0080) {
273 Utf8Char
->Utf8_1
= (UINT8
) (UnicodeByte0
& 0x7f);
276 } else if (Unicode
< 0x0800) {
278 // byte sequence: high -> low
279 // Utf8_2[0], Utf8_2[1]
281 Utf8Char
->Utf8_2
[1] = (UINT8
) ((UnicodeByte0
& 0x3f) + 0x80);
282 Utf8Char
->Utf8_2
[0] = (UINT8
) ((((UnicodeByte1
<< 2) + (UnicodeByte0
>> 6)) & 0x1f) + 0xc0);
288 // byte sequence: high -> low
289 // Utf8_3[0], Utf8_3[1], Utf8_3[2]
291 Utf8Char
->Utf8_3
[2] = (UINT8
) ((UnicodeByte0
& 0x3f) + 0x80);
292 Utf8Char
->Utf8_3
[1] = (UINT8
) ((((UnicodeByte1
<< 2) + (UnicodeByte0
>> 6)) & 0x3f) + 0x80);
293 Utf8Char
->Utf8_3
[0] = (UINT8
) (((UnicodeByte1
>> 4) & 0x0f) + 0xe0);
301 Check if input string is valid VT-UTF8 string.
303 @param TerminalDevice The terminal device.
304 @param WString The input string.
306 @retval EFI_SUCCESS If all input characters are valid.
311 IN TERMINAL_DEV
*TerminalDevice
,
316 // to utf8, all kind of characters are supported.