]>
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 - 2009, 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.
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
);
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
[2 - Index
] = Temp
;
156 // reset *ValidBytes and Index to zero, let valid utf8 char search restart
176 Translate VT-UTF8 characters into one Unicode character.
179 Bits per Character | Unicode Character Range | Unicode Binary Encoding | UTF8 Binary Encoding
180 0-7 | 0x0000 - 0x007F | 00000000 0xxxxxxx | 0xxxxxxx
181 8-11 | 0x0080 - 0x07FF | 00000xxx xxxxxxxx | 110xxxxx 10xxxxxx
182 12-16 | 0x0800 - 0xFFFF | xxxxxxxx xxxxxxxx | 1110xxxx 10xxxxxx 10xxxxxx
185 @param Utf8Char VT-UTF8 character set needs translating.
186 @param ValidBytes The count of valid VT-UTF8 characters.
187 @param UnicodeChar Returned unicode character.
192 IN UTF8_CHAR Utf8Char
,
194 OUT CHAR16
*UnicodeChar
206 // translate utf8 code to unicode, in terminal standard,
207 // up to 3 bytes utf8 code is supported.
209 switch (ValidBytes
) {
212 // one-byte utf8 code
214 *UnicodeChar
= (UINT16
) Utf8Char
.Utf8_1
;
219 // two-byte utf8 code
221 Byte0
= Utf8Char
.Utf8_2
[0];
222 Byte1
= Utf8Char
.Utf8_2
[1];
224 UnicodeByte0
= (UINT8
) ((Byte1
<< 6) | (Byte0
& 0x3f));
225 UnicodeByte1
= (UINT8
) ((Byte1
>> 2) & 0x07);
226 *UnicodeChar
= (UINT16
) (UnicodeByte0
| (UnicodeByte1
<< 8));
231 // three-byte utf8 code
233 Byte0
= Utf8Char
.Utf8_3
[0];
234 Byte1
= Utf8Char
.Utf8_3
[1];
235 Byte2
= Utf8Char
.Utf8_3
[2];
237 UnicodeByte0
= (UINT8
) ((Byte1
<< 6) | (Byte0
& 0x3f));
238 UnicodeByte1
= (UINT8
) ((Byte2
<< 4) | ((Byte1
>> 2) & 0x0f));
239 *UnicodeChar
= (UINT16
) (UnicodeByte0
| (UnicodeByte1
<< 8));
249 Translate one Unicode character into VT-UTF8 characters.
252 Bits per Character | Unicode Character Range | Unicode Binary Encoding | UTF8 Binary Encoding
253 0-7 | 0x0000 - 0x007F | 00000000 0xxxxxxx | 0xxxxxxx
254 8-11 | 0x0080 - 0x07FF | 00000xxx xxxxxxxx | 110xxxxx 10xxxxxx
255 12-16 | 0x0800 - 0xFFFF | xxxxxxxx xxxxxxxx | 1110xxxx 10xxxxxx 10xxxxxx
258 @param Unicode Unicode character need translating.
259 @param Utf8Char Return VT-UTF8 character set.
260 @param ValidBytes The count of valid VT-UTF8 characters. If
261 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.