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.
26 VTUTF8RawDataToUnicode (
27 IN TERMINAL_DEV
*TerminalDevice
36 // pop the raw data out from the raw fifo,
37 // and translate it into unicode, then push
38 // the unicode into unicode fifo, until the raw fifo is empty.
40 while (!IsRawFiFoEmpty (TerminalDevice
)) {
42 GetOneValidUtf8Char (TerminalDevice
, &Utf8Char
, &ValidBytes
);
44 if (ValidBytes
< 1 || ValidBytes
> 3) {
48 Utf8ToUnicode (Utf8Char
, ValidBytes
, (CHAR16
*) &UnicodeChar
);
50 UnicodeFiFoInsertOneKey (TerminalDevice
, UnicodeChar
);
56 IN TERMINAL_DEV
*Utf8Device
,
57 OUT UTF8_CHAR
*Utf8Char
,
70 // if no valid Utf8 char is found in the RawFiFo,
71 // then *ValidBytes will be zero.
75 while (!IsRawFiFoEmpty (Utf8Device
)) {
77 RawFiFoRemoveOneKey (Utf8Device
, &Temp
);
79 switch (*ValidBytes
) {
82 if ((Temp
& 0x80) == 0) {
88 Utf8Char
->Utf8_1
= Temp
;
92 } else if ((Temp
& 0xe0) == 0xc0) {
98 Utf8Char
->Utf8_2
[1] = Temp
;
100 } else if ((Temp
& 0xf0) == 0xe0) {
102 // three-byte utf8 char
106 Utf8Char
->Utf8_3
[2] = Temp
;
112 // reset *ValidBytes to zero, let valid utf8 char search restart
120 if ((Temp
& 0xc0) == 0x80) {
122 Utf8Char
->Utf8_2
[0] = Temp
;
133 if ((Temp
& 0xc0) == 0x80) {
135 Utf8Char
->Utf8_3
[2 - Index
] = Temp
;
161 IN UTF8_CHAR Utf8Char
,
163 OUT CHAR16
*UnicodeChar
175 // translate utf8 code to unicode, in terminal standard,
176 // up to 3 bytes utf8 code is supported.
178 switch (ValidBytes
) {
181 // one-byte utf8 code
183 *UnicodeChar
= (UINT16
) Utf8Char
.Utf8_1
;
188 // two-byte utf8 code
190 Byte0
= Utf8Char
.Utf8_2
[0];
191 Byte1
= Utf8Char
.Utf8_2
[1];
193 UnicodeByte0
= (UINT8
) ((Byte1
<< 6) | (Byte0
& 0x3f));
194 UnicodeByte1
= (UINT8
) ((Byte1
>> 2) & 0x07);
195 *UnicodeChar
= (UINT16
) (UnicodeByte0
| (UnicodeByte1
<< 8));
200 // three-byte utf8 code
202 Byte0
= Utf8Char
.Utf8_3
[0];
203 Byte1
= Utf8Char
.Utf8_3
[1];
204 Byte2
= Utf8Char
.Utf8_3
[2];
206 UnicodeByte0
= (UINT8
) ((Byte1
<< 6) | (Byte0
& 0x3f));
207 UnicodeByte1
= (UINT8
) ((Byte2
<< 4) | ((Byte1
>> 2) & 0x0f));
208 *UnicodeChar
= (UINT16
) (UnicodeByte0
| (UnicodeByte1
<< 8));
220 OUT UTF8_CHAR
*Utf8Char
,
221 OUT UINT8
*ValidBytes
227 // translate unicode to utf8 code
229 UnicodeByte0
= (UINT8
) Unicode
;
230 UnicodeByte1
= (UINT8
) (Unicode
>> 8);
232 if (Unicode
< 0x0080) {
234 Utf8Char
->Utf8_1
= (UINT8
) (UnicodeByte0
& 0x7f);
237 } else if (Unicode
< 0x0800) {
239 // byte sequence: high -> low
240 // Utf8_2[0], Utf8_2[1]
242 Utf8Char
->Utf8_2
[1] = (UINT8
) ((UnicodeByte0
& 0x3f) + 0x80);
243 Utf8Char
->Utf8_2
[0] = (UINT8
) ((((UnicodeByte1
<< 2) + (UnicodeByte0
>> 6)) & 0x1f) + 0xc0);
249 // byte sequence: high -> low
250 // Utf8_3[0], Utf8_3[1], Utf8_3[2]
252 Utf8Char
->Utf8_3
[2] = (UINT8
) ((UnicodeByte0
& 0x3f) + 0x80);
253 Utf8Char
->Utf8_3
[1] = (UINT8
) ((((UnicodeByte1
<< 2) + (UnicodeByte0
>> 6)) & 0x3f) + 0x80);
254 Utf8Char
->Utf8_3
[0] = (UINT8
) (((UnicodeByte1
>> 4) & 0x0f) + 0xe0);
262 IN TERMINAL_DEV
*TerminalDevice
,
267 // to utf8, all kind of characters are supported.