]>
git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Console/TerminalDxe/vtutf8.c
2 Implementation translation among different code tyies.
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 VTUTF8RawDataToUnicode (
19 IN TERMINAL_DEV
*TerminalDevice
28 // pop the raw data out from the raw fifo,
29 // and translate it into unicode, then push
30 // the unicode into unicode fifo, until the raw fifo is empty.
32 while (!IsRawFiFoEmpty (TerminalDevice
)) {
34 GetOneValidUtf8Char (TerminalDevice
, &Utf8Char
, &ValidBytes
);
36 if (ValidBytes
< 1 || ValidBytes
> 3) {
40 Utf8ToUnicode (Utf8Char
, ValidBytes
, (CHAR16
*) &UnicodeChar
);
42 UnicodeFiFoInsertOneKey (TerminalDevice
, UnicodeChar
);
48 IN TERMINAL_DEV
*Utf8Device
,
49 OUT UTF8_CHAR
*Utf8Char
,
62 // if no valid Utf8 char is found in the RawFiFo,
63 // then *ValidBytes will be zero.
67 while (!IsRawFiFoEmpty (Utf8Device
)) {
69 RawFiFoRemoveOneKey (Utf8Device
, &Temp
);
71 switch (*ValidBytes
) {
74 if ((Temp
& 0x80) == 0) {
80 Utf8Char
->Utf8_1
= Temp
;
84 } else if ((Temp
& 0xe0) == 0xc0) {
90 Utf8Char
->Utf8_2
[1] = Temp
;
92 } else if ((Temp
& 0xf0) == 0xe0) {
94 // three-byte utf8 char
98 Utf8Char
->Utf8_3
[2] = Temp
;
104 // reset *ValidBytes to zero, let valid utf8 char search restart
112 if ((Temp
& 0xc0) == 0x80) {
114 Utf8Char
->Utf8_2
[0] = Temp
;
125 if ((Temp
& 0xc0) == 0x80) {
127 Utf8Char
->Utf8_3
[2 - Index
] = Temp
;
153 IN UTF8_CHAR Utf8Char
,
155 OUT CHAR16
*UnicodeChar
167 // translate utf8 code to unicode, in terminal standard,
168 // up to 3 bytes utf8 code is supported.
170 switch (ValidBytes
) {
173 // one-byte utf8 code
175 *UnicodeChar
= (UINT16
) Utf8Char
.Utf8_1
;
180 // two-byte utf8 code
182 Byte0
= Utf8Char
.Utf8_2
[0];
183 Byte1
= Utf8Char
.Utf8_2
[1];
185 UnicodeByte0
= (UINT8
) ((Byte1
<< 6) | (Byte0
& 0x3f));
186 UnicodeByte1
= (UINT8
) ((Byte1
>> 2) & 0x07);
187 *UnicodeChar
= (UINT16
) (UnicodeByte0
| (UnicodeByte1
<< 8));
192 // three-byte utf8 code
194 Byte0
= Utf8Char
.Utf8_3
[0];
195 Byte1
= Utf8Char
.Utf8_3
[1];
196 Byte2
= Utf8Char
.Utf8_3
[2];
198 UnicodeByte0
= (UINT8
) ((Byte1
<< 6) | (Byte0
& 0x3f));
199 UnicodeByte1
= (UINT8
) ((Byte2
<< 4) | ((Byte1
>> 2) & 0x0f));
200 *UnicodeChar
= (UINT16
) (UnicodeByte0
| (UnicodeByte1
<< 8));
212 OUT UTF8_CHAR
*Utf8Char
,
213 OUT UINT8
*ValidBytes
219 // translate unicode to utf8 code
221 UnicodeByte0
= (UINT8
) Unicode
;
222 UnicodeByte1
= (UINT8
) (Unicode
>> 8);
224 if (Unicode
< 0x0080) {
226 Utf8Char
->Utf8_1
= (UINT8
) (UnicodeByte0
& 0x7f);
229 } else if (Unicode
< 0x0800) {
231 // byte sequence: high -> low
232 // Utf8_2[0], Utf8_2[1]
234 Utf8Char
->Utf8_2
[1] = (UINT8
) ((UnicodeByte0
& 0x3f) + 0x80);
235 Utf8Char
->Utf8_2
[0] = (UINT8
) ((((UnicodeByte1
<< 2) + (UnicodeByte0
>> 6)) & 0x1f) + 0xc0);
241 // byte sequence: high -> low
242 // Utf8_3[0], Utf8_3[1], Utf8_3[2]
244 Utf8Char
->Utf8_3
[2] = (UINT8
) ((UnicodeByte0
& 0x3f) + 0x80);
245 Utf8Char
->Utf8_3
[1] = (UINT8
) ((((UnicodeByte1
<< 2) + (UnicodeByte0
>> 6)) & 0x3f) + 0x80);
246 Utf8Char
->Utf8_3
[0] = (UINT8
) (((UnicodeByte1
>> 4) & 0x0f) + 0xe0);
254 IN TERMINAL_DEV
*TerminalDevice
,
259 // to utf8, all kind of characters are supported.