TianoCore EDK2 master
Loading...
Searching...
No Matches
Vtutf8.c
Go to the documentation of this file.
1
9#include "Terminal.h"
10
18VOID
20 IN TERMINAL_DEV *TerminalDevice
21 )
22{
23 UTF8_CHAR Utf8Char;
24 UINT8 ValidBytes;
25 UINT16 UnicodeChar;
26
27 ValidBytes = 0;
28 //
29 // pop the raw data out from the raw fifo,
30 // and translate it into unicode, then push
31 // the unicode into unicode fifo, until the raw fifo is empty.
32 //
33 while (!IsRawFiFoEmpty (TerminalDevice) && !IsUnicodeFiFoFull (TerminalDevice)) {
34 GetOneValidUtf8Char (TerminalDevice, &Utf8Char, &ValidBytes);
35
36 if ((ValidBytes < 1) || (ValidBytes > 3)) {
37 continue;
38 }
39
40 Utf8ToUnicode (Utf8Char, ValidBytes, (CHAR16 *)&UnicodeChar);
41
42 UnicodeFiFoInsertOneKey (TerminalDevice, UnicodeChar);
43 }
44}
45
55VOID
57 IN TERMINAL_DEV *Utf8Device,
58 OUT UTF8_CHAR *Utf8Char,
59 OUT UINT8 *ValidBytes
60 )
61{
62 UINT8 Temp;
63 UINT8 Index;
64 BOOLEAN FetchFlag;
65
66 Temp = 0;
67 Index = 0;
68 FetchFlag = TRUE;
69
70 //
71 // if no valid Utf8 char is found in the RawFiFo,
72 // then *ValidBytes will be zero.
73 //
74 *ValidBytes = 0;
75
76 while (!IsRawFiFoEmpty (Utf8Device)) {
77 RawFiFoRemoveOneKey (Utf8Device, &Temp);
78
79 switch (*ValidBytes) {
80 case 0:
81 if ((Temp & 0x80) == 0) {
82 //
83 // one-byte utf8 char
84 //
85 *ValidBytes = 1;
86
87 Utf8Char->Utf8_1 = Temp;
88
89 FetchFlag = FALSE;
90 } else if ((Temp & 0xe0) == 0xc0) {
91 //
92 // two-byte utf8 char
93 //
94 *ValidBytes = 2;
95
96 Utf8Char->Utf8_2[1] = Temp;
97 } else if ((Temp & 0xf0) == 0xe0) {
98 //
99 // three-byte utf8 char
100 //
101 *ValidBytes = 3;
102
103 Utf8Char->Utf8_3[2] = Temp;
104
105 Index++;
106 } else {
107 //
108 // reset *ValidBytes to zero, let valid utf8 char search restart
109 //
110 *ValidBytes = 0;
111 }
112
113 break;
114
115 case 2:
116 //
117 // two-byte utf8 char go on
118 //
119 if ((Temp & 0xc0) == 0x80) {
120 Utf8Char->Utf8_2[0] = Temp;
121
122 FetchFlag = FALSE;
123 } else {
124 *ValidBytes = 0;
125 }
126
127 break;
128
129 case 3:
130 //
131 // three-byte utf8 char go on
132 //
133 if ((Temp & 0xc0) == 0x80) {
134 if (Index == 1) {
135 Utf8Char->Utf8_3[1] = Temp;
136 Index++;
137 } else {
138 Utf8Char->Utf8_3[0] = Temp;
139 FetchFlag = FALSE;
140 }
141 } else {
142 //
143 // reset *ValidBytes and Index to zero, let valid utf8 char search restart
144 //
145 *ValidBytes = 0;
146 Index = 0;
147 }
148
149 break;
150
151 default:
152 break;
153 }
154
155 if (!FetchFlag) {
156 break;
157 }
158 }
159
160 return;
161}
162
178VOID
180 IN UTF8_CHAR Utf8Char,
181 IN UINT8 ValidBytes,
182 OUT CHAR16 *UnicodeChar
183 )
184{
185 UINT8 UnicodeByte0;
186 UINT8 UnicodeByte1;
187 UINT8 Byte0;
188 UINT8 Byte1;
189 UINT8 Byte2;
190
191 *UnicodeChar = 0;
192
193 //
194 // translate utf8 code to unicode, in terminal standard,
195 // up to 3 bytes utf8 code is supported.
196 //
197 switch (ValidBytes) {
198 case 1:
199 //
200 // one-byte utf8 code
201 //
202 *UnicodeChar = (UINT16)Utf8Char.Utf8_1;
203 break;
204
205 case 2:
206 //
207 // two-byte utf8 code
208 //
209 Byte0 = Utf8Char.Utf8_2[0];
210 Byte1 = Utf8Char.Utf8_2[1];
211
212 UnicodeByte0 = (UINT8)((Byte1 << 6) | (Byte0 & 0x3f));
213 UnicodeByte1 = (UINT8)((Byte1 >> 2) & 0x07);
214 *UnicodeChar = (UINT16)(UnicodeByte0 | (UnicodeByte1 << 8));
215 break;
216
217 case 3:
218 //
219 // three-byte utf8 code
220 //
221 Byte0 = Utf8Char.Utf8_3[0];
222 Byte1 = Utf8Char.Utf8_3[1];
223 Byte2 = Utf8Char.Utf8_3[2];
224
225 UnicodeByte0 = (UINT8)((Byte1 << 6) | (Byte0 & 0x3f));
226 UnicodeByte1 = (UINT8)((Byte2 << 4) | ((Byte1 >> 2) & 0x0f));
227 *UnicodeChar = (UINT16)(UnicodeByte0 | (UnicodeByte1 << 8));
228
229 default:
230 break;
231 }
232
233 return;
234}
235
252VOID
254 IN CHAR16 Unicode,
255 OUT UTF8_CHAR *Utf8Char,
256 OUT UINT8 *ValidBytes
257 )
258{
259 UINT8 UnicodeByte0;
260 UINT8 UnicodeByte1;
261
262 //
263 // translate unicode to utf8 code
264 //
265 UnicodeByte0 = (UINT8)Unicode;
266 UnicodeByte1 = (UINT8)(Unicode >> 8);
267
268 if (Unicode < 0x0080) {
269 Utf8Char->Utf8_1 = (UINT8)(UnicodeByte0 & 0x7f);
270 *ValidBytes = 1;
271 } else if (Unicode < 0x0800) {
272 //
273 // byte sequence: high -> low
274 // Utf8_2[0], Utf8_2[1]
275 //
276 Utf8Char->Utf8_2[1] = (UINT8)((UnicodeByte0 & 0x3f) + 0x80);
277 Utf8Char->Utf8_2[0] = (UINT8)((((UnicodeByte1 << 2) + (UnicodeByte0 >> 6)) & 0x1f) + 0xc0);
278
279 *ValidBytes = 2;
280 } else {
281 //
282 // byte sequence: high -> low
283 // Utf8_3[0], Utf8_3[1], Utf8_3[2]
284 //
285 Utf8Char->Utf8_3[2] = (UINT8)((UnicodeByte0 & 0x3f) + 0x80);
286 Utf8Char->Utf8_3[1] = (UINT8)((((UnicodeByte1 << 2) + (UnicodeByte0 >> 6)) & 0x3f) + 0x80);
287 Utf8Char->Utf8_3[0] = (UINT8)(((UnicodeByte1 >> 4) & 0x0f) + 0xe0);
288
289 *ValidBytes = 3;
290 }
291}
292
304 IN TERMINAL_DEV *TerminalDevice,
305 IN CHAR16 *WString
306 )
307{
308 //
309 // to utf8, all kind of characters are supported.
310 //
311 return EFI_SUCCESS;
312}
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
BOOLEAN RawFiFoRemoveOneKey(TERMINAL_DEV *TerminalDevice, UINT8 *Output)
BOOLEAN IsUnicodeFiFoFull(TERMINAL_DEV *TerminalDevice)
BOOLEAN UnicodeFiFoInsertOneKey(TERMINAL_DEV *TerminalDevice, UINT16 Input)
BOOLEAN IsRawFiFoEmpty(TERMINAL_DEV *TerminalDevice)
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
EFI_STATUS VTUTF8TestString(IN TERMINAL_DEV *TerminalDevice, IN CHAR16 *WString)
Definition: Vtutf8.c:303
VOID VTUTF8RawDataToUnicode(IN TERMINAL_DEV *TerminalDevice)
Definition: Vtutf8.c:19
VOID UnicodeToUtf8(IN CHAR16 Unicode, OUT UTF8_CHAR *Utf8Char, OUT UINT8 *ValidBytes)
Definition: Vtutf8.c:253
VOID Utf8ToUnicode(IN UTF8_CHAR Utf8Char, IN UINT8 ValidBytes, OUT CHAR16 *UnicodeChar)
Definition: Vtutf8.c:179
VOID GetOneValidUtf8Char(IN TERMINAL_DEV *Utf8Device, OUT UTF8_CHAR *Utf8Char, OUT UINT8 *ValidBytes)
Definition: Vtutf8.c:56