]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Console/TerminalDxe/Vtutf8.c
Update the copyright notice format
[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
e5eed7d3
HT
4Copyright (c) 2006 - 2009, Intel Corporation. All rights reserved.<BR>\r
5This program and the accompanying materials\r
fb0b259e 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
5c998646 18 Translate all VT-UTF8 characters in the Raw FIFI into unicode characters,\r
8fd98315 19 and insert them into Unicode FIFO.\r
20\r
21 @param TerminalDevice The terminal device.\r
22\r
8fd98315 23**/\r
95276127 24VOID\r
25VTUTF8RawDataToUnicode (\r
26 IN TERMINAL_DEV *TerminalDevice\r
27 )\r
28{\r
29 UTF8_CHAR Utf8Char;\r
30 UINT8 ValidBytes;\r
31 UINT16 UnicodeChar;\r
32\r
33 ValidBytes = 0;\r
34 //\r
35 // pop the raw data out from the raw fifo,\r
36 // and translate it into unicode, then push\r
37 // the unicode into unicode fifo, until the raw fifo is empty.\r
38 //\r
39 while (!IsRawFiFoEmpty (TerminalDevice)) {\r
40\r
41 GetOneValidUtf8Char (TerminalDevice, &Utf8Char, &ValidBytes);\r
42\r
43 if (ValidBytes < 1 || ValidBytes > 3) {\r
44 continue;\r
45 }\r
46\r
47 Utf8ToUnicode (Utf8Char, ValidBytes, (CHAR16 *) &UnicodeChar);\r
48\r
49 UnicodeFiFoInsertOneKey (TerminalDevice, UnicodeChar);\r
50 }\r
51}\r
52\r
8fd98315 53/**\r
54 Get one valid VT-UTF8 characters set from Raw Data FIFO.\r
55\r
56 @param Utf8Device The terminal device.\r
57 @param Utf8Char Returned valid VT-UTF8 characters set.\r
5c998646 58 @param ValidBytes The count of returned VT-VTF8 characters.\r
8fd98315 59 If ValidBytes is zero, no valid VT-UTF8 returned.\r
60\r
8fd98315 61**/\r
95276127 62VOID\r
63GetOneValidUtf8Char (\r
64 IN TERMINAL_DEV *Utf8Device,\r
65 OUT UTF8_CHAR *Utf8Char,\r
66 OUT UINT8 *ValidBytes\r
67 )\r
68{\r
69 UINT8 Temp;\r
70 UINT8 Index;\r
71 BOOLEAN FetchFlag;\r
72\r
73 Temp = 0;\r
74 Index = 0;\r
75 FetchFlag = TRUE;\r
76\r
77 //\r
78 // if no valid Utf8 char is found in the RawFiFo,\r
79 // then *ValidBytes will be zero.\r
80 //\r
81 *ValidBytes = 0;\r
82\r
83 while (!IsRawFiFoEmpty (Utf8Device)) {\r
84\r
85 RawFiFoRemoveOneKey (Utf8Device, &Temp);\r
86\r
87 switch (*ValidBytes) {\r
88\r
89 case 0:\r
90 if ((Temp & 0x80) == 0) {\r
91 //\r
92 // one-byte utf8 char\r
93 //\r
94 *ValidBytes = 1;\r
95\r
96 Utf8Char->Utf8_1 = Temp;\r
97\r
98 FetchFlag = FALSE;\r
99\r
100 } else if ((Temp & 0xe0) == 0xc0) {\r
101 //\r
102 // two-byte utf8 char\r
103 //\r
104 *ValidBytes = 2;\r
105\r
106 Utf8Char->Utf8_2[1] = Temp;\r
107\r
108 } else if ((Temp & 0xf0) == 0xe0) {\r
109 //\r
110 // three-byte utf8 char\r
111 //\r
112 *ValidBytes = 3;\r
113\r
114 Utf8Char->Utf8_3[2] = Temp;\r
115\r
116 Index++;\r
117\r
118 } else {\r
119 //\r
120 // reset *ValidBytes to zero, let valid utf8 char search restart\r
121 //\r
122 *ValidBytes = 0;\r
123 }\r
124\r
125 break;\r
126\r
127 case 2:\r
5c998646 128 //\r
129 // two-byte utf8 char go on\r
130 //\r
95276127 131 if ((Temp & 0xc0) == 0x80) {\r
132\r
133 Utf8Char->Utf8_2[0] = Temp;\r
134\r
135 FetchFlag = FALSE;\r
136\r
137 } else {\r
138\r
139 *ValidBytes = 0;\r
140 }\r
141 break;\r
142\r
143 case 3:\r
5c998646 144 //\r
145 // three-byte utf8 char go on\r
146 //\r
95276127 147 if ((Temp & 0xc0) == 0x80) {\r
148\r
149 Utf8Char->Utf8_3[2 - Index] = Temp;\r
150 Index++;\r
5c998646 151 if (Index > 2) {\r
95276127 152 FetchFlag = FALSE;\r
153 }\r
154 } else {\r
5c998646 155 //\r
156 // reset *ValidBytes and Index to zero, let valid utf8 char search restart\r
157 //\r
95276127 158 *ValidBytes = 0;\r
159 Index = 0;\r
160 }\r
161 break;\r
162\r
163 default:\r
164 break;\r
165 }\r
166\r
167 if (!FetchFlag) {\r
168 break;\r
169 }\r
170 }\r
171\r
172 return ;\r
173}\r
174\r
5c998646 175/**\r
8fd98315 176 Translate VT-UTF8 characters into one Unicode character.\r
177\r
178 UTF8 Encoding Table\r
179 Bits per Character | Unicode Character Range | Unicode Binary Encoding | UTF8 Binary Encoding\r
180 0-7 | 0x0000 - 0x007F | 00000000 0xxxxxxx | 0xxxxxxx\r
181 8-11 | 0x0080 - 0x07FF | 00000xxx xxxxxxxx | 110xxxxx 10xxxxxx\r
182 12-16 | 0x0800 - 0xFFFF | xxxxxxxx xxxxxxxx | 1110xxxx 10xxxxxx 10xxxxxx\r
183\r
184\r
185 @param Utf8Char VT-UTF8 character set needs translating.\r
186 @param ValidBytes The count of valid VT-UTF8 characters.\r
5c998646 187 @param UnicodeChar Returned unicode character.\r
8fd98315 188\r
189**/\r
95276127 190VOID\r
191Utf8ToUnicode (\r
192 IN UTF8_CHAR Utf8Char,\r
193 IN UINT8 ValidBytes,\r
194 OUT CHAR16 *UnicodeChar\r
195 )\r
196{\r
197 UINT8 UnicodeByte0;\r
198 UINT8 UnicodeByte1;\r
199 UINT8 Byte0;\r
200 UINT8 Byte1;\r
201 UINT8 Byte2;\r
202\r
203 *UnicodeChar = 0;\r
204\r
205 //\r
206 // translate utf8 code to unicode, in terminal standard,\r
207 // up to 3 bytes utf8 code is supported.\r
208 //\r
209 switch (ValidBytes) {\r
210 case 1:\r
211 //\r
212 // one-byte utf8 code\r
213 //\r
214 *UnicodeChar = (UINT16) Utf8Char.Utf8_1;\r
215 break;\r
216\r
217 case 2:\r
218 //\r
219 // two-byte utf8 code\r
220 //\r
221 Byte0 = Utf8Char.Utf8_2[0];\r
222 Byte1 = Utf8Char.Utf8_2[1];\r
223\r
224 UnicodeByte0 = (UINT8) ((Byte1 << 6) | (Byte0 & 0x3f));\r
225 UnicodeByte1 = (UINT8) ((Byte1 >> 2) & 0x07);\r
226 *UnicodeChar = (UINT16) (UnicodeByte0 | (UnicodeByte1 << 8));\r
227 break;\r
228\r
229 case 3:\r
230 //\r
231 // three-byte utf8 code\r
232 //\r
233 Byte0 = Utf8Char.Utf8_3[0];\r
234 Byte1 = Utf8Char.Utf8_3[1];\r
235 Byte2 = Utf8Char.Utf8_3[2];\r
236\r
237 UnicodeByte0 = (UINT8) ((Byte1 << 6) | (Byte0 & 0x3f));\r
238 UnicodeByte1 = (UINT8) ((Byte2 << 4) | ((Byte1 >> 2) & 0x0f));\r
239 *UnicodeChar = (UINT16) (UnicodeByte0 | (UnicodeByte1 << 8));\r
240\r
241 default:\r
242 break;\r
243 }\r
244\r
245 return ;\r
246}\r
247\r
5c998646 248/**\r
8fd98315 249 Translate one Unicode character into VT-UTF8 characters.\r
250\r
251 UTF8 Encoding Table\r
252 Bits per Character | Unicode Character Range | Unicode Binary Encoding | UTF8 Binary Encoding\r
253 0-7 | 0x0000 - 0x007F | 00000000 0xxxxxxx | 0xxxxxxx\r
254 8-11 | 0x0080 - 0x07FF | 00000xxx xxxxxxxx | 110xxxxx 10xxxxxx\r
255 12-16 | 0x0800 - 0xFFFF | xxxxxxxx xxxxxxxx | 1110xxxx 10xxxxxx 10xxxxxx\r
256\r
257\r
258 @param Unicode Unicode character need translating.\r
259 @param Utf8Char Return VT-UTF8 character set.\r
260 @param ValidBytes The count of valid VT-UTF8 characters. If\r
261 ValidBytes is zero, no valid VT-UTF8 returned.\r
8fd98315 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
5c998646 312 @param WString The input string.\r
313\r
8fd98315 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