]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Console/TerminalDxe/Vtutf8.c
ECC clean up.
[mirror_edk2.git] / MdeModulePkg / Universal / Console / TerminalDxe / Vtutf8.c
CommitLineData
e49ef433 1/** @file\r
8fd98315 2 Implementation of translation upon VT-UTF8.\r
95276127 3\r
fb0b259e 4Copyright (c) 2006, Intel Corporation. <BR>\r
5All rights reserved. This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
95276127 9\r
fb0b259e 10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
95276127 12\r
fb0b259e 13**/\r
95276127 14\r
95276127 15#include "Terminal.h"\r
16\r
8fd98315 17/**\r
18 Translate all VT-UTF8 characters in the Raw FIFI into unicode characters, \r
19 and insert them into Unicode FIFO.\r
20\r
21 @param TerminalDevice The terminal device.\r
22\r
23 @return None.\r
24\r
25**/\r
95276127 26VOID\r
27VTUTF8RawDataToUnicode (\r
28 IN TERMINAL_DEV *TerminalDevice\r
29 )\r
30{\r
31 UTF8_CHAR Utf8Char;\r
32 UINT8 ValidBytes;\r
33 UINT16 UnicodeChar;\r
34\r
35 ValidBytes = 0;\r
36 //\r
37 // pop the raw data out from the raw fifo,\r
38 // and translate it into unicode, then push\r
39 // the unicode into unicode fifo, until the raw fifo is empty.\r
40 //\r
41 while (!IsRawFiFoEmpty (TerminalDevice)) {\r
42\r
43 GetOneValidUtf8Char (TerminalDevice, &Utf8Char, &ValidBytes);\r
44\r
45 if (ValidBytes < 1 || ValidBytes > 3) {\r
46 continue;\r
47 }\r
48\r
49 Utf8ToUnicode (Utf8Char, ValidBytes, (CHAR16 *) &UnicodeChar);\r
50\r
51 UnicodeFiFoInsertOneKey (TerminalDevice, UnicodeChar);\r
52 }\r
53}\r
54\r
8fd98315 55/**\r
56 Get one valid VT-UTF8 characters set from Raw Data FIFO.\r
57\r
58 @param Utf8Device The terminal device.\r
59 @param Utf8Char Returned valid VT-UTF8 characters set.\r
60 @param ValidBytes The count of returned VT-VTF8 characters. \r
61 If ValidBytes is zero, no valid VT-UTF8 returned.\r
62\r
8fd98315 63**/\r
95276127 64VOID\r
65GetOneValidUtf8Char (\r
66 IN TERMINAL_DEV *Utf8Device,\r
67 OUT UTF8_CHAR *Utf8Char,\r
68 OUT UINT8 *ValidBytes\r
69 )\r
70{\r
71 UINT8 Temp;\r
72 UINT8 Index;\r
73 BOOLEAN FetchFlag;\r
74\r
75 Temp = 0;\r
76 Index = 0;\r
77 FetchFlag = TRUE;\r
78\r
79 //\r
80 // if no valid Utf8 char is found in the RawFiFo,\r
81 // then *ValidBytes will be zero.\r
82 //\r
83 *ValidBytes = 0;\r
84\r
85 while (!IsRawFiFoEmpty (Utf8Device)) {\r
86\r
87 RawFiFoRemoveOneKey (Utf8Device, &Temp);\r
88\r
89 switch (*ValidBytes) {\r
90\r
91 case 0:\r
92 if ((Temp & 0x80) == 0) {\r
93 //\r
94 // one-byte utf8 char\r
95 //\r
96 *ValidBytes = 1;\r
97\r
98 Utf8Char->Utf8_1 = Temp;\r
99\r
100 FetchFlag = FALSE;\r
101\r
102 } else if ((Temp & 0xe0) == 0xc0) {\r
103 //\r
104 // two-byte utf8 char\r
105 //\r
106 *ValidBytes = 2;\r
107\r
108 Utf8Char->Utf8_2[1] = Temp;\r
109\r
110 } else if ((Temp & 0xf0) == 0xe0) {\r
111 //\r
112 // three-byte utf8 char\r
113 //\r
114 *ValidBytes = 3;\r
115\r
116 Utf8Char->Utf8_3[2] = Temp;\r
117\r
118 Index++;\r
119\r
120 } else {\r
121 //\r
122 // reset *ValidBytes to zero, let valid utf8 char search restart\r
123 //\r
124 *ValidBytes = 0;\r
125 }\r
126\r
127 break;\r
128\r
129 case 2:\r
130 if ((Temp & 0xc0) == 0x80) {\r
131\r
132 Utf8Char->Utf8_2[0] = Temp;\r
133\r
134 FetchFlag = FALSE;\r
135\r
136 } else {\r
137\r
138 *ValidBytes = 0;\r
139 }\r
140 break;\r
141\r
142 case 3:\r
143 if ((Temp & 0xc0) == 0x80) {\r
144\r
145 Utf8Char->Utf8_3[2 - Index] = Temp;\r
146 Index++;\r
147 if (Index == 3) {\r
148 FetchFlag = FALSE;\r
149 }\r
150 } else {\r
151\r
152 *ValidBytes = 0;\r
153 Index = 0;\r
154 }\r
155 break;\r
156\r
157 default:\r
158 break;\r
159 }\r
160\r
161 if (!FetchFlag) {\r
162 break;\r
163 }\r
164 }\r
165\r
166 return ;\r
167}\r
168\r
8fd98315 169/** \r
170 Translate VT-UTF8 characters into one Unicode character.\r
171\r
172 UTF8 Encoding Table\r
173 Bits per Character | Unicode Character Range | Unicode Binary Encoding | UTF8 Binary Encoding\r
174 0-7 | 0x0000 - 0x007F | 00000000 0xxxxxxx | 0xxxxxxx\r
175 8-11 | 0x0080 - 0x07FF | 00000xxx xxxxxxxx | 110xxxxx 10xxxxxx\r
176 12-16 | 0x0800 - 0xFFFF | xxxxxxxx xxxxxxxx | 1110xxxx 10xxxxxx 10xxxxxx\r
177\r
178\r
179 @param Utf8Char VT-UTF8 character set needs translating.\r
180 @param ValidBytes The count of valid VT-UTF8 characters.\r
181 @param UnicodeChar Returned unicode character. \r
182 \r
183 @return None.\r
184\r
185**/\r
95276127 186VOID\r
187Utf8ToUnicode (\r
188 IN UTF8_CHAR Utf8Char,\r
189 IN UINT8 ValidBytes,\r
190 OUT CHAR16 *UnicodeChar\r
191 )\r
192{\r
193 UINT8 UnicodeByte0;\r
194 UINT8 UnicodeByte1;\r
195 UINT8 Byte0;\r
196 UINT8 Byte1;\r
197 UINT8 Byte2;\r
198\r
199 *UnicodeChar = 0;\r
200\r
201 //\r
202 // translate utf8 code to unicode, in terminal standard,\r
203 // up to 3 bytes utf8 code is supported.\r
204 //\r
205 switch (ValidBytes) {\r
206 case 1:\r
207 //\r
208 // one-byte utf8 code\r
209 //\r
210 *UnicodeChar = (UINT16) Utf8Char.Utf8_1;\r
211 break;\r
212\r
213 case 2:\r
214 //\r
215 // two-byte utf8 code\r
216 //\r
217 Byte0 = Utf8Char.Utf8_2[0];\r
218 Byte1 = Utf8Char.Utf8_2[1];\r
219\r
220 UnicodeByte0 = (UINT8) ((Byte1 << 6) | (Byte0 & 0x3f));\r
221 UnicodeByte1 = (UINT8) ((Byte1 >> 2) & 0x07);\r
222 *UnicodeChar = (UINT16) (UnicodeByte0 | (UnicodeByte1 << 8));\r
223 break;\r
224\r
225 case 3:\r
226 //\r
227 // three-byte utf8 code\r
228 //\r
229 Byte0 = Utf8Char.Utf8_3[0];\r
230 Byte1 = Utf8Char.Utf8_3[1];\r
231 Byte2 = Utf8Char.Utf8_3[2];\r
232\r
233 UnicodeByte0 = (UINT8) ((Byte1 << 6) | (Byte0 & 0x3f));\r
234 UnicodeByte1 = (UINT8) ((Byte2 << 4) | ((Byte1 >> 2) & 0x0f));\r
235 *UnicodeChar = (UINT16) (UnicodeByte0 | (UnicodeByte1 << 8));\r
236\r
237 default:\r
238 break;\r
239 }\r
240\r
241 return ;\r
242}\r
243\r
8fd98315 244/** \r
245 Translate one Unicode character into VT-UTF8 characters.\r
246\r
247 UTF8 Encoding Table\r
248 Bits per Character | Unicode Character Range | Unicode Binary Encoding | UTF8 Binary Encoding\r
249 0-7 | 0x0000 - 0x007F | 00000000 0xxxxxxx | 0xxxxxxx\r
250 8-11 | 0x0080 - 0x07FF | 00000xxx xxxxxxxx | 110xxxxx 10xxxxxx\r
251 12-16 | 0x0800 - 0xFFFF | xxxxxxxx xxxxxxxx | 1110xxxx 10xxxxxx 10xxxxxx\r
252\r
253\r
254 @param Unicode Unicode character need translating.\r
255 @param Utf8Char Return VT-UTF8 character set.\r
256 @param ValidBytes The count of valid VT-UTF8 characters. If\r
257 ValidBytes is zero, no valid VT-UTF8 returned.\r
258 \r
259 @return None.\r
260\r
261**/\r
95276127 262VOID\r
263UnicodeToUtf8 (\r
264 IN CHAR16 Unicode,\r
265 OUT UTF8_CHAR *Utf8Char,\r
266 OUT UINT8 *ValidBytes\r
267 )\r
268{\r
269 UINT8 UnicodeByte0;\r
270 UINT8 UnicodeByte1;\r
271 //\r
272 // translate unicode to utf8 code\r
273 //\r
274 UnicodeByte0 = (UINT8) Unicode;\r
275 UnicodeByte1 = (UINT8) (Unicode >> 8);\r
276\r
277 if (Unicode < 0x0080) {\r
278\r
279 Utf8Char->Utf8_1 = (UINT8) (UnicodeByte0 & 0x7f);\r
280 *ValidBytes = 1;\r
281\r
282 } else if (Unicode < 0x0800) {\r
283 //\r
284 // byte sequence: high -> low\r
285 // Utf8_2[0], Utf8_2[1]\r
286 //\r
287 Utf8Char->Utf8_2[1] = (UINT8) ((UnicodeByte0 & 0x3f) + 0x80);\r
288 Utf8Char->Utf8_2[0] = (UINT8) ((((UnicodeByte1 << 2) + (UnicodeByte0 >> 6)) & 0x1f) + 0xc0);\r
289\r
290 *ValidBytes = 2;\r
291\r
292 } else {\r
293 //\r
294 // byte sequence: high -> low\r
295 // Utf8_3[0], Utf8_3[1], Utf8_3[2]\r
296 //\r
297 Utf8Char->Utf8_3[2] = (UINT8) ((UnicodeByte0 & 0x3f) + 0x80);\r
298 Utf8Char->Utf8_3[1] = (UINT8) ((((UnicodeByte1 << 2) + (UnicodeByte0 >> 6)) & 0x3f) + 0x80);\r
299 Utf8Char->Utf8_3[0] = (UINT8) (((UnicodeByte1 >> 4) & 0x0f) + 0xe0);\r
300\r
301 *ValidBytes = 3;\r
302 }\r
303}\r
304\r
8fd98315 305\r
306/**\r
307 Check if input string is valid VT-UTF8 string.\r
308\r
309 @param TerminalDevice The terminal device.\r
310 @param WString The input string. \r
311 \r
312 @retval EFI_SUCCESS If all input characters are valid.\r
313\r
314**/\r
95276127 315EFI_STATUS\r
316VTUTF8TestString (\r
317 IN TERMINAL_DEV *TerminalDevice,\r
318 IN CHAR16 *WString\r
319 )\r
320{\r
321 //\r
322 // to utf8, all kind of characters are supported.\r
323 //\r
324 return EFI_SUCCESS;\r
325}\r