]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Console/TerminalDxe/vtutf8.c
Check in following modules,
[mirror_edk2.git] / MdeModulePkg / Universal / Console / TerminalDxe / vtutf8.c
CommitLineData
95276127 1/*++\r
2\r
3Copyright (c) 2006, Intel Corporation \r
4All rights reserved. This program and the accompanying materials \r
5are licensed and made available under the terms and conditions of the BSD License \r
6which accompanies this distribution. The full text of the license may be found at \r
7http://opensource.org/licenses/bsd-license.php \r
8 \r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
11\r
12Module Name:\r
13\r
14 vtutf8.c\r
15 \r
16Abstract: \r
17 \r
18\r
19Revision History\r
20--*/\r
21\r
22\r
23//\r
24// Include common header file for this module.\r
25//\r
26#include "CommonHeader.h"\r
27\r
28#include "Terminal.h"\r
29\r
30VOID\r
31VTUTF8RawDataToUnicode (\r
32 IN TERMINAL_DEV *TerminalDevice\r
33 )\r
34{\r
35 UTF8_CHAR Utf8Char;\r
36 UINT8 ValidBytes;\r
37 UINT16 UnicodeChar;\r
38\r
39 ValidBytes = 0;\r
40 //\r
41 // pop the raw data out from the raw fifo,\r
42 // and translate it into unicode, then push\r
43 // the unicode into unicode fifo, until the raw fifo is empty.\r
44 //\r
45 while (!IsRawFiFoEmpty (TerminalDevice)) {\r
46\r
47 GetOneValidUtf8Char (TerminalDevice, &Utf8Char, &ValidBytes);\r
48\r
49 if (ValidBytes < 1 || ValidBytes > 3) {\r
50 continue;\r
51 }\r
52\r
53 Utf8ToUnicode (Utf8Char, ValidBytes, (CHAR16 *) &UnicodeChar);\r
54\r
55 UnicodeFiFoInsertOneKey (TerminalDevice, UnicodeChar);\r
56 }\r
57}\r
58\r
59VOID\r
60GetOneValidUtf8Char (\r
61 IN TERMINAL_DEV *Utf8Device,\r
62 OUT UTF8_CHAR *Utf8Char,\r
63 OUT UINT8 *ValidBytes\r
64 )\r
65{\r
66 UINT8 Temp;\r
67 UINT8 Index;\r
68 BOOLEAN FetchFlag;\r
69\r
70 Temp = 0;\r
71 Index = 0;\r
72 FetchFlag = TRUE;\r
73\r
74 //\r
75 // if no valid Utf8 char is found in the RawFiFo,\r
76 // then *ValidBytes will be zero.\r
77 //\r
78 *ValidBytes = 0;\r
79\r
80 while (!IsRawFiFoEmpty (Utf8Device)) {\r
81\r
82 RawFiFoRemoveOneKey (Utf8Device, &Temp);\r
83\r
84 switch (*ValidBytes) {\r
85\r
86 case 0:\r
87 if ((Temp & 0x80) == 0) {\r
88 //\r
89 // one-byte utf8 char\r
90 //\r
91 *ValidBytes = 1;\r
92\r
93 Utf8Char->Utf8_1 = Temp;\r
94\r
95 FetchFlag = FALSE;\r
96\r
97 } else if ((Temp & 0xe0) == 0xc0) {\r
98 //\r
99 // two-byte utf8 char\r
100 //\r
101 *ValidBytes = 2;\r
102\r
103 Utf8Char->Utf8_2[1] = Temp;\r
104\r
105 } else if ((Temp & 0xf0) == 0xe0) {\r
106 //\r
107 // three-byte utf8 char\r
108 //\r
109 *ValidBytes = 3;\r
110\r
111 Utf8Char->Utf8_3[2] = Temp;\r
112\r
113 Index++;\r
114\r
115 } else {\r
116 //\r
117 // reset *ValidBytes to zero, let valid utf8 char search restart\r
118 //\r
119 *ValidBytes = 0;\r
120 }\r
121\r
122 break;\r
123\r
124 case 2:\r
125 if ((Temp & 0xc0) == 0x80) {\r
126\r
127 Utf8Char->Utf8_2[0] = Temp;\r
128\r
129 FetchFlag = FALSE;\r
130\r
131 } else {\r
132\r
133 *ValidBytes = 0;\r
134 }\r
135 break;\r
136\r
137 case 3:\r
138 if ((Temp & 0xc0) == 0x80) {\r
139\r
140 Utf8Char->Utf8_3[2 - Index] = Temp;\r
141 Index++;\r
142 if (Index == 3) {\r
143 FetchFlag = FALSE;\r
144 }\r
145 } else {\r
146\r
147 *ValidBytes = 0;\r
148 Index = 0;\r
149 }\r
150 break;\r
151\r
152 default:\r
153 break;\r
154 }\r
155\r
156 if (!FetchFlag) {\r
157 break;\r
158 }\r
159 }\r
160\r
161 return ;\r
162}\r
163\r
164VOID\r
165Utf8ToUnicode (\r
166 IN UTF8_CHAR Utf8Char,\r
167 IN UINT8 ValidBytes,\r
168 OUT CHAR16 *UnicodeChar\r
169 )\r
170{\r
171 UINT8 UnicodeByte0;\r
172 UINT8 UnicodeByte1;\r
173 UINT8 Byte0;\r
174 UINT8 Byte1;\r
175 UINT8 Byte2;\r
176\r
177 *UnicodeChar = 0;\r
178\r
179 //\r
180 // translate utf8 code to unicode, in terminal standard,\r
181 // up to 3 bytes utf8 code is supported.\r
182 //\r
183 switch (ValidBytes) {\r
184 case 1:\r
185 //\r
186 // one-byte utf8 code\r
187 //\r
188 *UnicodeChar = (UINT16) Utf8Char.Utf8_1;\r
189 break;\r
190\r
191 case 2:\r
192 //\r
193 // two-byte utf8 code\r
194 //\r
195 Byte0 = Utf8Char.Utf8_2[0];\r
196 Byte1 = Utf8Char.Utf8_2[1];\r
197\r
198 UnicodeByte0 = (UINT8) ((Byte1 << 6) | (Byte0 & 0x3f));\r
199 UnicodeByte1 = (UINT8) ((Byte1 >> 2) & 0x07);\r
200 *UnicodeChar = (UINT16) (UnicodeByte0 | (UnicodeByte1 << 8));\r
201 break;\r
202\r
203 case 3:\r
204 //\r
205 // three-byte utf8 code\r
206 //\r
207 Byte0 = Utf8Char.Utf8_3[0];\r
208 Byte1 = Utf8Char.Utf8_3[1];\r
209 Byte2 = Utf8Char.Utf8_3[2];\r
210\r
211 UnicodeByte0 = (UINT8) ((Byte1 << 6) | (Byte0 & 0x3f));\r
212 UnicodeByte1 = (UINT8) ((Byte2 << 4) | ((Byte1 >> 2) & 0x0f));\r
213 *UnicodeChar = (UINT16) (UnicodeByte0 | (UnicodeByte1 << 8));\r
214\r
215 default:\r
216 break;\r
217 }\r
218\r
219 return ;\r
220}\r
221\r
222VOID\r
223UnicodeToUtf8 (\r
224 IN CHAR16 Unicode,\r
225 OUT UTF8_CHAR *Utf8Char,\r
226 OUT UINT8 *ValidBytes\r
227 )\r
228{\r
229 UINT8 UnicodeByte0;\r
230 UINT8 UnicodeByte1;\r
231 //\r
232 // translate unicode to utf8 code\r
233 //\r
234 UnicodeByte0 = (UINT8) Unicode;\r
235 UnicodeByte1 = (UINT8) (Unicode >> 8);\r
236\r
237 if (Unicode < 0x0080) {\r
238\r
239 Utf8Char->Utf8_1 = (UINT8) (UnicodeByte0 & 0x7f);\r
240 *ValidBytes = 1;\r
241\r
242 } else if (Unicode < 0x0800) {\r
243 //\r
244 // byte sequence: high -> low\r
245 // Utf8_2[0], Utf8_2[1]\r
246 //\r
247 Utf8Char->Utf8_2[1] = (UINT8) ((UnicodeByte0 & 0x3f) + 0x80);\r
248 Utf8Char->Utf8_2[0] = (UINT8) ((((UnicodeByte1 << 2) + (UnicodeByte0 >> 6)) & 0x1f) + 0xc0);\r
249\r
250 *ValidBytes = 2;\r
251\r
252 } else {\r
253 //\r
254 // byte sequence: high -> low\r
255 // Utf8_3[0], Utf8_3[1], Utf8_3[2]\r
256 //\r
257 Utf8Char->Utf8_3[2] = (UINT8) ((UnicodeByte0 & 0x3f) + 0x80);\r
258 Utf8Char->Utf8_3[1] = (UINT8) ((((UnicodeByte1 << 2) + (UnicodeByte0 >> 6)) & 0x3f) + 0x80);\r
259 Utf8Char->Utf8_3[0] = (UINT8) (((UnicodeByte1 >> 4) & 0x0f) + 0xe0);\r
260\r
261 *ValidBytes = 3;\r
262 }\r
263}\r
264\r
265EFI_STATUS\r
266VTUTF8TestString (\r
267 IN TERMINAL_DEV *TerminalDevice,\r
268 IN CHAR16 *WString\r
269 )\r
270{\r
271 //\r
272 // to utf8, all kind of characters are supported.\r
273 //\r
274 return EFI_SUCCESS;\r
275}\r