]> git.proxmox.com Git - mirror_edk2.git/blame - EdkModulePkg/Universal/Console/Terminal/Dxe/vtutf8.c
Initial import.
[mirror_edk2.git] / EdkModulePkg / Universal / Console / Terminal / Dxe / vtutf8.c
CommitLineData
878ddf1f 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#include "Terminal.h"\r
24\r
25VOID\r
26VTUTF8RawDataToUnicode (\r
27 IN TERMINAL_DEV *TerminalDevice\r
28 )\r
29{\r
30 UTF8_CHAR Utf8Char;\r
31 UINT8 ValidBytes;\r
32 UINT16 UnicodeChar;\r
33\r
34 ValidBytes = 0;\r
35 //\r
36 // pop the raw data out from the raw fifo,\r
37 // and translate it into unicode, then push\r
38 // the unicode into unicode fifo, until the raw fifo is empty.\r
39 //\r
40 while (!IsRawFiFoEmpty (TerminalDevice)) {\r
41\r
42 GetOneValidUtf8Char (TerminalDevice, &Utf8Char, &ValidBytes);\r
43\r
44 if (ValidBytes < 1 || ValidBytes > 3) {\r
45 continue;\r
46 }\r
47\r
48 Utf8ToUnicode (Utf8Char, ValidBytes, (CHAR16 *) &UnicodeChar);\r
49\r
50 UnicodeFiFoInsertOneKey (TerminalDevice, UnicodeChar);\r
51 }\r
52}\r
53\r
54VOID\r
55GetOneValidUtf8Char (\r
56 IN TERMINAL_DEV *Utf8Device,\r
57 OUT UTF8_CHAR *Utf8Char,\r
58 OUT UINT8 *ValidBytes\r
59 )\r
60{\r
61 UINT8 Temp;\r
62 UINT8 Index;\r
63 BOOLEAN FetchFlag;\r
64\r
65 Temp = 0;\r
66 Index = 0;\r
67 FetchFlag = TRUE;\r
68\r
69 //\r
70 // if no valid Utf8 char is found in the RawFiFo,\r
71 // then *ValidBytes will be zero.\r
72 //\r
73 *ValidBytes = 0;\r
74\r
75 while (!IsRawFiFoEmpty (Utf8Device)) {\r
76\r
77 RawFiFoRemoveOneKey (Utf8Device, &Temp);\r
78\r
79 switch (*ValidBytes) {\r
80\r
81 case 0:\r
82 if ((Temp & 0x80) == 0) {\r
83 //\r
84 // one-byte utf8 char\r
85 //\r
86 *ValidBytes = 1;\r
87\r
88 Utf8Char->Utf8_1 = Temp;\r
89\r
90 FetchFlag = FALSE;\r
91\r
92 } else if ((Temp & 0xe0) == 0xc0) {\r
93 //\r
94 // two-byte utf8 char\r
95 //\r
96 *ValidBytes = 2;\r
97\r
98 Utf8Char->Utf8_2[1] = Temp;\r
99\r
100 } else if ((Temp & 0xf0) == 0xe0) {\r
101 //\r
102 // three-byte utf8 char\r
103 //\r
104 *ValidBytes = 3;\r
105\r
106 Utf8Char->Utf8_3[2] = Temp;\r
107\r
108 Index++;\r
109\r
110 } else {\r
111 //\r
112 // reset *ValidBytes to zero, let valid utf8 char search restart\r
113 //\r
114 *ValidBytes = 0;\r
115 }\r
116\r
117 break;\r
118\r
119 case 2:\r
120 if ((Temp & 0xc0) == 0x80) {\r
121\r
122 Utf8Char->Utf8_2[0] = Temp;\r
123\r
124 FetchFlag = FALSE;\r
125\r
126 } else {\r
127\r
128 *ValidBytes = 0;\r
129 }\r
130 break;\r
131\r
132 case 3:\r
133 if ((Temp & 0xc0) == 0x80) {\r
134\r
135 Utf8Char->Utf8_3[2 - Index] = Temp;\r
136 Index++;\r
137 if (Index == 3) {\r
138 FetchFlag = FALSE;\r
139 }\r
140 } else {\r
141\r
142 *ValidBytes = 0;\r
143 Index = 0;\r
144 }\r
145 break;\r
146\r
147 default:\r
148 break;\r
149 }\r
150\r
151 if (!FetchFlag) {\r
152 break;\r
153 }\r
154 }\r
155\r
156 return ;\r
157}\r
158\r
159VOID\r
160Utf8ToUnicode (\r
161 IN UTF8_CHAR Utf8Char,\r
162 IN UINT8 ValidBytes,\r
163 OUT CHAR16 *UnicodeChar\r
164 )\r
165{\r
166 UINT8 UnicodeByte0;\r
167 UINT8 UnicodeByte1;\r
168 UINT8 Byte0;\r
169 UINT8 Byte1;\r
170 UINT8 Byte2;\r
171\r
172 *UnicodeChar = 0;\r
173\r
174 //\r
175 // translate utf8 code to unicode, in terminal standard,\r
176 // up to 3 bytes utf8 code is supported.\r
177 //\r
178 switch (ValidBytes) {\r
179 case 1:\r
180 //\r
181 // one-byte utf8 code\r
182 //\r
183 *UnicodeChar = (UINT16) Utf8Char.Utf8_1;\r
184 break;\r
185\r
186 case 2:\r
187 //\r
188 // two-byte utf8 code\r
189 //\r
190 Byte0 = Utf8Char.Utf8_2[0];\r
191 Byte1 = Utf8Char.Utf8_2[1];\r
192\r
193 UnicodeByte0 = (UINT8) ((Byte1 << 6) | (Byte0 & 0x3f));\r
194 UnicodeByte1 = (UINT8) ((Byte1 >> 2) & 0x07);\r
195 *UnicodeChar = (UINT16) (UnicodeByte0 | (UnicodeByte1 << 8));\r
196 break;\r
197\r
198 case 3:\r
199 //\r
200 // three-byte utf8 code\r
201 //\r
202 Byte0 = Utf8Char.Utf8_3[0];\r
203 Byte1 = Utf8Char.Utf8_3[1];\r
204 Byte2 = Utf8Char.Utf8_3[2];\r
205\r
206 UnicodeByte0 = (UINT8) ((Byte1 << 6) | (Byte0 & 0x3f));\r
207 UnicodeByte1 = (UINT8) ((Byte2 << 4) | ((Byte1 >> 2) & 0x0f));\r
208 *UnicodeChar = (UINT16) (UnicodeByte0 | (UnicodeByte1 << 8));\r
209\r
210 default:\r
211 break;\r
212 }\r
213\r
214 return ;\r
215}\r
216\r
217VOID\r
218UnicodeToUtf8 (\r
219 IN CHAR16 Unicode,\r
220 OUT UTF8_CHAR *Utf8Char,\r
221 OUT UINT8 *ValidBytes\r
222 )\r
223{\r
224 UINT8 UnicodeByte0;\r
225 UINT8 UnicodeByte1;\r
226 //\r
227 // translate unicode to utf8 code\r
228 //\r
229 UnicodeByte0 = (UINT8) Unicode;\r
230 UnicodeByte1 = (UINT8) (Unicode >> 8);\r
231\r
232 if (Unicode < 0x0080) {\r
233\r
234 Utf8Char->Utf8_1 = (UINT8) (UnicodeByte0 & 0x7f);\r
235 *ValidBytes = 1;\r
236\r
237 } else if (Unicode < 0x0800) {\r
238 //\r
239 // byte sequence: high -> low\r
240 // Utf8_2[0], Utf8_2[1]\r
241 //\r
242 Utf8Char->Utf8_2[1] = (UINT8) ((UnicodeByte0 & 0x3f) + 0x80);\r
243 Utf8Char->Utf8_2[0] = (UINT8) ((((UnicodeByte1 << 2) + (UnicodeByte0 >> 6)) & 0x1f) + 0xc0);\r
244\r
245 *ValidBytes = 2;\r
246\r
247 } else {\r
248 //\r
249 // byte sequence: high -> low\r
250 // Utf8_3[0], Utf8_3[1], Utf8_3[2]\r
251 //\r
252 Utf8Char->Utf8_3[2] = (UINT8) ((UnicodeByte0 & 0x3f) + 0x80);\r
253 Utf8Char->Utf8_3[1] = (UINT8) ((((UnicodeByte1 << 2) + (UnicodeByte0 >> 6)) & 0x3f) + 0x80);\r
254 Utf8Char->Utf8_3[0] = (UINT8) (((UnicodeByte1 >> 4) & 0x0f) + 0xe0);\r
255\r
256 *ValidBytes = 3;\r
257 }\r
258}\r
259\r
260EFI_STATUS\r
261VTUTF8TestString (\r
262 IN TERMINAL_DEV *TerminalDevice,\r
263 IN CHAR16 *WString\r
264 )\r
265{\r
266 //\r
267 // to utf8, all kind of characters are supported.\r
268 //\r
269 return EFI_SUCCESS;\r
270}\r