]>
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 - 2010, Intel Corporation. All rights reserved.<BR>
5 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
) && !IsUnicodeFiFoFull (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
129 // two-byte utf8 char go on
131 if ((Temp
& 0xc0) == 0x80) {
133 Utf8Char
->Utf8_2
[0] = Temp
;
145 // three-byte utf8 char go on
147 if ((Temp
& 0xc0) == 0x80) {
149 Utf8Char
->Utf8_3
[1] = Temp
;
152 Utf8Char
->Utf8_3
[0] = Temp
;
157 // reset *ValidBytes and Index to zero, let valid utf8 char search restart
177 Translate VT-UTF8 characters into one Unicode character.
180 Bits per Character | Unicode Character Range | Unicode Binary Encoding | UTF8 Binary Encoding
181 0-7 | 0x0000 - 0x007F | 00000000 0xxxxxxx | 0xxxxxxx
182 8-11 | 0x0080 - 0x07FF | 00000xxx xxxxxxxx | 110xxxxx 10xxxxxx
183 12-16 | 0x0800 - 0xFFFF | xxxxxxxx xxxxxxxx | 1110xxxx 10xxxxxx 10xxxxxx
186 @param Utf8Char VT-UTF8 character set needs translating.
187 @param ValidBytes The count of valid VT-UTF8 characters.
188 @param UnicodeChar Returned unicode character.
193 IN UTF8_CHAR Utf8Char
,
195 OUT CHAR16
*UnicodeChar
207 // translate utf8 code to unicode, in terminal standard,
208 // up to 3 bytes utf8 code is supported.
210 switch (ValidBytes
) {
213 // one-byte utf8 code
215 *UnicodeChar
= (UINT16
) Utf8Char
.Utf8_1
;
220 // two-byte utf8 code
222 Byte0
= Utf8Char
.Utf8_2
[0];
223 Byte1
= Utf8Char
.Utf8_2
[1];
225 UnicodeByte0
= (UINT8
) ((Byte1
<< 6) | (Byte0
& 0x3f));
226 UnicodeByte1
= (UINT8
) ((Byte1
>> 2) & 0x07);
227 *UnicodeChar
= (UINT16
) (UnicodeByte0
| (UnicodeByte1
<< 8));
232 // three-byte utf8 code
234 Byte0
= Utf8Char
.Utf8_3
[0];
235 Byte1
= Utf8Char
.Utf8_3
[1];
236 Byte2
= Utf8Char
.Utf8_3
[2];
238 UnicodeByte0
= (UINT8
) ((Byte1
<< 6) | (Byte0
& 0x3f));
239 UnicodeByte1
= (UINT8
) ((Byte2
<< 4) | ((Byte1
>> 2) & 0x0f));
240 *UnicodeChar
= (UINT16
) (UnicodeByte0
| (UnicodeByte1
<< 8));
250 Translate one Unicode character into VT-UTF8 characters.
253 Bits per Character | Unicode Character Range | Unicode Binary Encoding | UTF8 Binary Encoding
254 0-7 | 0x0000 - 0x007F | 00000000 0xxxxxxx | 0xxxxxxx
255 8-11 | 0x0080 - 0x07FF | 00000xxx xxxxxxxx | 110xxxxx 10xxxxxx
256 12-16 | 0x0800 - 0xFFFF | xxxxxxxx xxxxxxxx | 1110xxxx 10xxxxxx 10xxxxxx
259 @param Unicode Unicode character need translating.
260 @param Utf8Char Return VT-UTF8 character set.
261 @param ValidBytes The count of valid VT-UTF8 characters. If
262 ValidBytes is zero, no valid VT-UTF8 returned.
268 OUT UTF8_CHAR
*Utf8Char
,
269 OUT UINT8
*ValidBytes
275 // translate unicode to utf8 code
277 UnicodeByte0
= (UINT8
) Unicode
;
278 UnicodeByte1
= (UINT8
) (Unicode
>> 8);
280 if (Unicode
< 0x0080) {
282 Utf8Char
->Utf8_1
= (UINT8
) (UnicodeByte0
& 0x7f);
285 } else if (Unicode
< 0x0800) {
287 // byte sequence: high -> low
288 // Utf8_2[0], Utf8_2[1]
290 Utf8Char
->Utf8_2
[1] = (UINT8
) ((UnicodeByte0
& 0x3f) + 0x80);
291 Utf8Char
->Utf8_2
[0] = (UINT8
) ((((UnicodeByte1
<< 2) + (UnicodeByte0
>> 6)) & 0x1f) + 0xc0);
297 // byte sequence: high -> low
298 // Utf8_3[0], Utf8_3[1], Utf8_3[2]
300 Utf8Char
->Utf8_3
[2] = (UINT8
) ((UnicodeByte0
& 0x3f) + 0x80);
301 Utf8Char
->Utf8_3
[1] = (UINT8
) ((((UnicodeByte1
<< 2) + (UnicodeByte0
>> 6)) & 0x3f) + 0x80);
302 Utf8Char
->Utf8_3
[0] = (UINT8
) (((UnicodeByte1
>> 4) & 0x0f) + 0xe0);
310 Check if input string is valid VT-UTF8 string.
312 @param TerminalDevice The terminal device.
313 @param WString The input string.
315 @retval EFI_SUCCESS If all input characters are valid.
320 IN TERMINAL_DEV
*TerminalDevice
,
325 // to utf8, all kind of characters are supported.