]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Console/TerminalDxe/Vtutf8.c
Added some functions header.
[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
63 @retval None.\r
64\r
65**/\r
95276127 66VOID\r
67GetOneValidUtf8Char (\r
68 IN TERMINAL_DEV *Utf8Device,\r
69 OUT UTF8_CHAR *Utf8Char,\r
70 OUT UINT8 *ValidBytes\r
71 )\r
72{\r
73 UINT8 Temp;\r
74 UINT8 Index;\r
75 BOOLEAN FetchFlag;\r
76\r
77 Temp = 0;\r
78 Index = 0;\r
79 FetchFlag = TRUE;\r
80\r
81 //\r
82 // if no valid Utf8 char is found in the RawFiFo,\r
83 // then *ValidBytes will be zero.\r
84 //\r
85 *ValidBytes = 0;\r
86\r
87 while (!IsRawFiFoEmpty (Utf8Device)) {\r
88\r
89 RawFiFoRemoveOneKey (Utf8Device, &Temp);\r
90\r
91 switch (*ValidBytes) {\r
92\r
93 case 0:\r
94 if ((Temp & 0x80) == 0) {\r
95 //\r
96 // one-byte utf8 char\r
97 //\r
98 *ValidBytes = 1;\r
99\r
100 Utf8Char->Utf8_1 = Temp;\r
101\r
102 FetchFlag = FALSE;\r
103\r
104 } else if ((Temp & 0xe0) == 0xc0) {\r
105 //\r
106 // two-byte utf8 char\r
107 //\r
108 *ValidBytes = 2;\r
109\r
110 Utf8Char->Utf8_2[1] = Temp;\r
111\r
112 } else if ((Temp & 0xf0) == 0xe0) {\r
113 //\r
114 // three-byte utf8 char\r
115 //\r
116 *ValidBytes = 3;\r
117\r
118 Utf8Char->Utf8_3[2] = Temp;\r
119\r
120 Index++;\r
121\r
122 } else {\r
123 //\r
124 // reset *ValidBytes to zero, let valid utf8 char search restart\r
125 //\r
126 *ValidBytes = 0;\r
127 }\r
128\r
129 break;\r
130\r
131 case 2:\r
132 if ((Temp & 0xc0) == 0x80) {\r
133\r
134 Utf8Char->Utf8_2[0] = Temp;\r
135\r
136 FetchFlag = FALSE;\r
137\r
138 } else {\r
139\r
140 *ValidBytes = 0;\r
141 }\r
142 break;\r
143\r
144 case 3:\r
145 if ((Temp & 0xc0) == 0x80) {\r
146\r
147 Utf8Char->Utf8_3[2 - Index] = Temp;\r
148 Index++;\r
149 if (Index == 3) {\r
150 FetchFlag = FALSE;\r
151 }\r
152 } else {\r
153\r
154 *ValidBytes = 0;\r
155 Index = 0;\r
156 }\r
157 break;\r
158\r
159 default:\r
160 break;\r
161 }\r
162\r
163 if (!FetchFlag) {\r
164 break;\r
165 }\r
166 }\r
167\r
168 return ;\r
169}\r
170\r
8fd98315 171/** \r
172 Translate VT-UTF8 characters into one Unicode character.\r
173\r
174 UTF8 Encoding Table\r
175 Bits per Character | Unicode Character Range | Unicode Binary Encoding | UTF8 Binary Encoding\r
176 0-7 | 0x0000 - 0x007F | 00000000 0xxxxxxx | 0xxxxxxx\r
177 8-11 | 0x0080 - 0x07FF | 00000xxx xxxxxxxx | 110xxxxx 10xxxxxx\r
178 12-16 | 0x0800 - 0xFFFF | xxxxxxxx xxxxxxxx | 1110xxxx 10xxxxxx 10xxxxxx\r
179\r
180\r
181 @param Utf8Char VT-UTF8 character set needs translating.\r
182 @param ValidBytes The count of valid VT-UTF8 characters.\r
183 @param UnicodeChar Returned unicode character. \r
184 \r
185 @return None.\r
186\r
187**/\r
95276127 188VOID\r
189Utf8ToUnicode (\r
190 IN UTF8_CHAR Utf8Char,\r
191 IN UINT8 ValidBytes,\r
192 OUT CHAR16 *UnicodeChar\r
193 )\r
194{\r
195 UINT8 UnicodeByte0;\r
196 UINT8 UnicodeByte1;\r
197 UINT8 Byte0;\r
198 UINT8 Byte1;\r
199 UINT8 Byte2;\r
200\r
201 *UnicodeChar = 0;\r
202\r
203 //\r
204 // translate utf8 code to unicode, in terminal standard,\r
205 // up to 3 bytes utf8 code is supported.\r
206 //\r
207 switch (ValidBytes) {\r
208 case 1:\r
209 //\r
210 // one-byte utf8 code\r
211 //\r
212 *UnicodeChar = (UINT16) Utf8Char.Utf8_1;\r
213 break;\r
214\r
215 case 2:\r
216 //\r
217 // two-byte utf8 code\r
218 //\r
219 Byte0 = Utf8Char.Utf8_2[0];\r
220 Byte1 = Utf8Char.Utf8_2[1];\r
221\r
222 UnicodeByte0 = (UINT8) ((Byte1 << 6) | (Byte0 & 0x3f));\r
223 UnicodeByte1 = (UINT8) ((Byte1 >> 2) & 0x07);\r
224 *UnicodeChar = (UINT16) (UnicodeByte0 | (UnicodeByte1 << 8));\r
225 break;\r
226\r
227 case 3:\r
228 //\r
229 // three-byte utf8 code\r
230 //\r
231 Byte0 = Utf8Char.Utf8_3[0];\r
232 Byte1 = Utf8Char.Utf8_3[1];\r
233 Byte2 = Utf8Char.Utf8_3[2];\r
234\r
235 UnicodeByte0 = (UINT8) ((Byte1 << 6) | (Byte0 & 0x3f));\r
236 UnicodeByte1 = (UINT8) ((Byte2 << 4) | ((Byte1 >> 2) & 0x0f));\r
237 *UnicodeChar = (UINT16) (UnicodeByte0 | (UnicodeByte1 << 8));\r
238\r
239 default:\r
240 break;\r
241 }\r
242\r
243 return ;\r
244}\r
245\r
8fd98315 246/** \r
247 Translate one Unicode character into VT-UTF8 characters.\r
248\r
249 UTF8 Encoding Table\r
250 Bits per Character | Unicode Character Range | Unicode Binary Encoding | UTF8 Binary Encoding\r
251 0-7 | 0x0000 - 0x007F | 00000000 0xxxxxxx | 0xxxxxxx\r
252 8-11 | 0x0080 - 0x07FF | 00000xxx xxxxxxxx | 110xxxxx 10xxxxxx\r
253 12-16 | 0x0800 - 0xFFFF | xxxxxxxx xxxxxxxx | 1110xxxx 10xxxxxx 10xxxxxx\r
254\r
255\r
256 @param Unicode Unicode character need translating.\r
257 @param Utf8Char Return VT-UTF8 character set.\r
258 @param ValidBytes The count of valid VT-UTF8 characters. If\r
259 ValidBytes is zero, no valid VT-UTF8 returned.\r
260 \r
261 @return None.\r
262\r
263**/\r
95276127 264VOID\r
265UnicodeToUtf8 (\r
266 IN CHAR16 Unicode,\r
267 OUT UTF8_CHAR *Utf8Char,\r
268 OUT UINT8 *ValidBytes\r
269 )\r
270{\r
271 UINT8 UnicodeByte0;\r
272 UINT8 UnicodeByte1;\r
273 //\r
274 // translate unicode to utf8 code\r
275 //\r
276 UnicodeByte0 = (UINT8) Unicode;\r
277 UnicodeByte1 = (UINT8) (Unicode >> 8);\r
278\r
279 if (Unicode < 0x0080) {\r
280\r
281 Utf8Char->Utf8_1 = (UINT8) (UnicodeByte0 & 0x7f);\r
282 *ValidBytes = 1;\r
283\r
284 } else if (Unicode < 0x0800) {\r
285 //\r
286 // byte sequence: high -> low\r
287 // Utf8_2[0], Utf8_2[1]\r
288 //\r
289 Utf8Char->Utf8_2[1] = (UINT8) ((UnicodeByte0 & 0x3f) + 0x80);\r
290 Utf8Char->Utf8_2[0] = (UINT8) ((((UnicodeByte1 << 2) + (UnicodeByte0 >> 6)) & 0x1f) + 0xc0);\r
291\r
292 *ValidBytes = 2;\r
293\r
294 } else {\r
295 //\r
296 // byte sequence: high -> low\r
297 // Utf8_3[0], Utf8_3[1], Utf8_3[2]\r
298 //\r
299 Utf8Char->Utf8_3[2] = (UINT8) ((UnicodeByte0 & 0x3f) + 0x80);\r
300 Utf8Char->Utf8_3[1] = (UINT8) ((((UnicodeByte1 << 2) + (UnicodeByte0 >> 6)) & 0x3f) + 0x80);\r
301 Utf8Char->Utf8_3[0] = (UINT8) (((UnicodeByte1 >> 4) & 0x0f) + 0xe0);\r
302\r
303 *ValidBytes = 3;\r
304 }\r
305}\r
306\r
8fd98315 307\r
308/**\r
309 Check if input string is valid VT-UTF8 string.\r
310\r
311 @param TerminalDevice The terminal device.\r
312 @param WString The input string. \r
313 \r
314 @retval EFI_SUCCESS If all input characters are valid.\r
315\r
316**/\r
95276127 317EFI_STATUS\r
318VTUTF8TestString (\r
319 IN TERMINAL_DEV *TerminalDevice,\r
320 IN CHAR16 *WString\r
321 )\r
322{\r
323 //\r
324 // to utf8, all kind of characters are supported.\r
325 //\r
326 return EFI_SUCCESS;\r
327}\r