]>
git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Console/TerminalDxe/vtutf8.c
3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
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
);
55 IN TERMINAL_DEV
*Utf8Device
,
56 OUT UTF8_CHAR
*Utf8Char
,
69 // if no valid Utf8 char is found in the RawFiFo,
70 // then *ValidBytes will be zero.
74 while (!IsRawFiFoEmpty (Utf8Device
)) {
76 RawFiFoRemoveOneKey (Utf8Device
, &Temp
);
78 switch (*ValidBytes
) {
81 if ((Temp
& 0x80) == 0) {
87 Utf8Char
->Utf8_1
= Temp
;
91 } else if ((Temp
& 0xe0) == 0xc0) {
97 Utf8Char
->Utf8_2
[1] = Temp
;
99 } else if ((Temp
& 0xf0) == 0xe0) {
101 // three-byte utf8 char
105 Utf8Char
->Utf8_3
[2] = Temp
;
111 // reset *ValidBytes to zero, let valid utf8 char search restart
119 if ((Temp
& 0xc0) == 0x80) {
121 Utf8Char
->Utf8_2
[0] = Temp
;
132 if ((Temp
& 0xc0) == 0x80) {
134 Utf8Char
->Utf8_3
[2 - Index
] = Temp
;
160 IN UTF8_CHAR Utf8Char
,
162 OUT CHAR16
*UnicodeChar
174 // translate utf8 code to unicode, in terminal standard,
175 // up to 3 bytes utf8 code is supported.
177 switch (ValidBytes
) {
180 // one-byte utf8 code
182 *UnicodeChar
= (UINT16
) Utf8Char
.Utf8_1
;
187 // two-byte utf8 code
189 Byte0
= Utf8Char
.Utf8_2
[0];
190 Byte1
= Utf8Char
.Utf8_2
[1];
192 UnicodeByte0
= (UINT8
) ((Byte1
<< 6) | (Byte0
& 0x3f));
193 UnicodeByte1
= (UINT8
) ((Byte1
>> 2) & 0x07);
194 *UnicodeChar
= (UINT16
) (UnicodeByte0
| (UnicodeByte1
<< 8));
199 // three-byte utf8 code
201 Byte0
= Utf8Char
.Utf8_3
[0];
202 Byte1
= Utf8Char
.Utf8_3
[1];
203 Byte2
= Utf8Char
.Utf8_3
[2];
205 UnicodeByte0
= (UINT8
) ((Byte1
<< 6) | (Byte0
& 0x3f));
206 UnicodeByte1
= (UINT8
) ((Byte2
<< 4) | ((Byte1
>> 2) & 0x0f));
207 *UnicodeChar
= (UINT16
) (UnicodeByte0
| (UnicodeByte1
<< 8));
219 OUT UTF8_CHAR
*Utf8Char
,
220 OUT UINT8
*ValidBytes
226 // translate unicode to utf8 code
228 UnicodeByte0
= (UINT8
) Unicode
;
229 UnicodeByte1
= (UINT8
) (Unicode
>> 8);
231 if (Unicode
< 0x0080) {
233 Utf8Char
->Utf8_1
= (UINT8
) (UnicodeByte0
& 0x7f);
236 } else if (Unicode
< 0x0800) {
238 // byte sequence: high -> low
239 // Utf8_2[0], Utf8_2[1]
241 Utf8Char
->Utf8_2
[1] = (UINT8
) ((UnicodeByte0
& 0x3f) + 0x80);
242 Utf8Char
->Utf8_2
[0] = (UINT8
) ((((UnicodeByte1
<< 2) + (UnicodeByte0
>> 6)) & 0x1f) + 0xc0);
248 // byte sequence: high -> low
249 // Utf8_3[0], Utf8_3[1], Utf8_3[2]
251 Utf8Char
->Utf8_3
[2] = (UINT8
) ((UnicodeByte0
& 0x3f) + 0x80);
252 Utf8Char
->Utf8_3
[1] = (UINT8
) ((((UnicodeByte1
<< 2) + (UnicodeByte0
>> 6)) & 0x3f) + 0x80);
253 Utf8Char
->Utf8_3
[0] = (UINT8
) (((UnicodeByte1
>> 4) & 0x0f) + 0xe0);
261 IN TERMINAL_DEV
*TerminalDevice
,
266 // to utf8, all kind of characters are supported.