]>
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.
68 IN TERMINAL_DEV
*Utf8Device
,
69 OUT UTF8_CHAR
*Utf8Char
,
82 // if no valid Utf8 char is found in the RawFiFo,
83 // then *ValidBytes will be zero.
87 while (!IsRawFiFoEmpty (Utf8Device
)) {
89 RawFiFoRemoveOneKey (Utf8Device
, &Temp
);
91 switch (*ValidBytes
) {
94 if ((Temp
& 0x80) == 0) {
100 Utf8Char
->Utf8_1
= Temp
;
104 } else if ((Temp
& 0xe0) == 0xc0) {
106 // two-byte utf8 char
110 Utf8Char
->Utf8_2
[1] = Temp
;
112 } else if ((Temp
& 0xf0) == 0xe0) {
114 // three-byte utf8 char
118 Utf8Char
->Utf8_3
[2] = Temp
;
124 // reset *ValidBytes to zero, let valid utf8 char search restart
132 if ((Temp
& 0xc0) == 0x80) {
134 Utf8Char
->Utf8_2
[0] = Temp
;
145 if ((Temp
& 0xc0) == 0x80) {
147 Utf8Char
->Utf8_3
[2 - Index
] = Temp
;
172 Translate VT-UTF8 characters into one Unicode character.
175 Bits per Character | Unicode Character Range | Unicode Binary Encoding | UTF8 Binary Encoding
176 0-7 | 0x0000 - 0x007F | 00000000 0xxxxxxx | 0xxxxxxx
177 8-11 | 0x0080 - 0x07FF | 00000xxx xxxxxxxx | 110xxxxx 10xxxxxx
178 12-16 | 0x0800 - 0xFFFF | xxxxxxxx xxxxxxxx | 1110xxxx 10xxxxxx 10xxxxxx
181 @param Utf8Char VT-UTF8 character set needs translating.
182 @param ValidBytes The count of valid VT-UTF8 characters.
183 @param UnicodeChar Returned unicode character.
190 IN UTF8_CHAR Utf8Char
,
192 OUT CHAR16
*UnicodeChar
204 // translate utf8 code to unicode, in terminal standard,
205 // up to 3 bytes utf8 code is supported.
207 switch (ValidBytes
) {
210 // one-byte utf8 code
212 *UnicodeChar
= (UINT16
) Utf8Char
.Utf8_1
;
217 // two-byte utf8 code
219 Byte0
= Utf8Char
.Utf8_2
[0];
220 Byte1
= Utf8Char
.Utf8_2
[1];
222 UnicodeByte0
= (UINT8
) ((Byte1
<< 6) | (Byte0
& 0x3f));
223 UnicodeByte1
= (UINT8
) ((Byte1
>> 2) & 0x07);
224 *UnicodeChar
= (UINT16
) (UnicodeByte0
| (UnicodeByte1
<< 8));
229 // three-byte utf8 code
231 Byte0
= Utf8Char
.Utf8_3
[0];
232 Byte1
= Utf8Char
.Utf8_3
[1];
233 Byte2
= Utf8Char
.Utf8_3
[2];
235 UnicodeByte0
= (UINT8
) ((Byte1
<< 6) | (Byte0
& 0x3f));
236 UnicodeByte1
= (UINT8
) ((Byte2
<< 4) | ((Byte1
>> 2) & 0x0f));
237 *UnicodeChar
= (UINT16
) (UnicodeByte0
| (UnicodeByte1
<< 8));
247 Translate one Unicode character into VT-UTF8 characters.
250 Bits per Character | Unicode Character Range | Unicode Binary Encoding | UTF8 Binary Encoding
251 0-7 | 0x0000 - 0x007F | 00000000 0xxxxxxx | 0xxxxxxx
252 8-11 | 0x0080 - 0x07FF | 00000xxx xxxxxxxx | 110xxxxx 10xxxxxx
253 12-16 | 0x0800 - 0xFFFF | xxxxxxxx xxxxxxxx | 1110xxxx 10xxxxxx 10xxxxxx
256 @param Unicode Unicode character need translating.
257 @param Utf8Char Return VT-UTF8 character set.
258 @param ValidBytes The count of valid VT-UTF8 characters. If
259 ValidBytes is zero, no valid VT-UTF8 returned.
267 OUT UTF8_CHAR
*Utf8Char
,
268 OUT UINT8
*ValidBytes
274 // translate unicode to utf8 code
276 UnicodeByte0
= (UINT8
) Unicode
;
277 UnicodeByte1
= (UINT8
) (Unicode
>> 8);
279 if (Unicode
< 0x0080) {
281 Utf8Char
->Utf8_1
= (UINT8
) (UnicodeByte0
& 0x7f);
284 } else if (Unicode
< 0x0800) {
286 // byte sequence: high -> low
287 // Utf8_2[0], Utf8_2[1]
289 Utf8Char
->Utf8_2
[1] = (UINT8
) ((UnicodeByte0
& 0x3f) + 0x80);
290 Utf8Char
->Utf8_2
[0] = (UINT8
) ((((UnicodeByte1
<< 2) + (UnicodeByte0
>> 6)) & 0x1f) + 0xc0);
296 // byte sequence: high -> low
297 // Utf8_3[0], Utf8_3[1], Utf8_3[2]
299 Utf8Char
->Utf8_3
[2] = (UINT8
) ((UnicodeByte0
& 0x3f) + 0x80);
300 Utf8Char
->Utf8_3
[1] = (UINT8
) ((((UnicodeByte1
<< 2) + (UnicodeByte0
>> 6)) & 0x3f) + 0x80);
301 Utf8Char
->Utf8_3
[0] = (UINT8
) (((UnicodeByte1
>> 4) & 0x0f) + 0xe0);
309 Check if input string is valid VT-UTF8 string.
311 @param TerminalDevice The terminal device.
312 @param WString The input string.
314 @retval EFI_SUCCESS If all input characters are valid.
319 IN TERMINAL_DEV
*TerminalDevice
,
324 // to utf8, all kind of characters are supported.