]>
Commit | Line | Data |
---|---|---|
1 | /** @file\r | |
2 | Implementation of translation upon VT-UTF8.\r | |
3 | \r | |
4 | Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r | |
5 | This program and the accompanying materials\r | |
6 | are licensed and made available under the terms and conditions of the BSD License\r | |
7 | which accompanies this distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | \r | |
13 | **/\r | |
14 | \r | |
15 | #include "Terminal.h"\r | |
16 | \r | |
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 | **/\r | |
24 | VOID\r | |
25 | VTUTF8RawDataToUnicode (\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) && !IsUnicodeFiFoFull (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 | |
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 | |
58 | @param ValidBytes The count of returned VT-VTF8 characters.\r | |
59 | If ValidBytes is zero, no valid VT-UTF8 returned.\r | |
60 | \r | |
61 | **/\r | |
62 | VOID\r | |
63 | GetOneValidUtf8Char (\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 | |
128 | //\r | |
129 | // two-byte utf8 char go on\r | |
130 | //\r | |
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 | |
144 | //\r | |
145 | // three-byte utf8 char go on\r | |
146 | //\r | |
147 | if ((Temp & 0xc0) == 0x80) {\r | |
148 | if (Index == 1) {\r | |
149 | Utf8Char->Utf8_3[1] = Temp;\r | |
150 | Index++;\r | |
151 | } else {\r | |
152 | Utf8Char->Utf8_3[0] = Temp;\r | |
153 | FetchFlag = FALSE;\r | |
154 | }\r | |
155 | } else {\r | |
156 | //\r | |
157 | // reset *ValidBytes and Index to zero, let valid utf8 char search restart\r | |
158 | //\r | |
159 | *ValidBytes = 0;\r | |
160 | Index = 0;\r | |
161 | }\r | |
162 | break;\r | |
163 | \r | |
164 | default:\r | |
165 | break;\r | |
166 | }\r | |
167 | \r | |
168 | if (!FetchFlag) {\r | |
169 | break;\r | |
170 | }\r | |
171 | }\r | |
172 | \r | |
173 | return ;\r | |
174 | }\r | |
175 | \r | |
176 | /**\r | |
177 | Translate VT-UTF8 characters into one Unicode character.\r | |
178 | \r | |
179 | UTF8 Encoding Table\r | |
180 | Bits per Character | Unicode Character Range | Unicode Binary Encoding | UTF8 Binary Encoding\r | |
181 | 0-7 | 0x0000 - 0x007F | 00000000 0xxxxxxx | 0xxxxxxx\r | |
182 | 8-11 | 0x0080 - 0x07FF | 00000xxx xxxxxxxx | 110xxxxx 10xxxxxx\r | |
183 | 12-16 | 0x0800 - 0xFFFF | xxxxxxxx xxxxxxxx | 1110xxxx 10xxxxxx 10xxxxxx\r | |
184 | \r | |
185 | \r | |
186 | @param Utf8Char VT-UTF8 character set needs translating.\r | |
187 | @param ValidBytes The count of valid VT-UTF8 characters.\r | |
188 | @param UnicodeChar Returned unicode character.\r | |
189 | \r | |
190 | **/\r | |
191 | VOID\r | |
192 | Utf8ToUnicode (\r | |
193 | IN UTF8_CHAR Utf8Char,\r | |
194 | IN UINT8 ValidBytes,\r | |
195 | OUT CHAR16 *UnicodeChar\r | |
196 | )\r | |
197 | {\r | |
198 | UINT8 UnicodeByte0;\r | |
199 | UINT8 UnicodeByte1;\r | |
200 | UINT8 Byte0;\r | |
201 | UINT8 Byte1;\r | |
202 | UINT8 Byte2;\r | |
203 | \r | |
204 | *UnicodeChar = 0;\r | |
205 | \r | |
206 | //\r | |
207 | // translate utf8 code to unicode, in terminal standard,\r | |
208 | // up to 3 bytes utf8 code is supported.\r | |
209 | //\r | |
210 | switch (ValidBytes) {\r | |
211 | case 1:\r | |
212 | //\r | |
213 | // one-byte utf8 code\r | |
214 | //\r | |
215 | *UnicodeChar = (UINT16) Utf8Char.Utf8_1;\r | |
216 | break;\r | |
217 | \r | |
218 | case 2:\r | |
219 | //\r | |
220 | // two-byte utf8 code\r | |
221 | //\r | |
222 | Byte0 = Utf8Char.Utf8_2[0];\r | |
223 | Byte1 = Utf8Char.Utf8_2[1];\r | |
224 | \r | |
225 | UnicodeByte0 = (UINT8) ((Byte1 << 6) | (Byte0 & 0x3f));\r | |
226 | UnicodeByte1 = (UINT8) ((Byte1 >> 2) & 0x07);\r | |
227 | *UnicodeChar = (UINT16) (UnicodeByte0 | (UnicodeByte1 << 8));\r | |
228 | break;\r | |
229 | \r | |
230 | case 3:\r | |
231 | //\r | |
232 | // three-byte utf8 code\r | |
233 | //\r | |
234 | Byte0 = Utf8Char.Utf8_3[0];\r | |
235 | Byte1 = Utf8Char.Utf8_3[1];\r | |
236 | Byte2 = Utf8Char.Utf8_3[2];\r | |
237 | \r | |
238 | UnicodeByte0 = (UINT8) ((Byte1 << 6) | (Byte0 & 0x3f));\r | |
239 | UnicodeByte1 = (UINT8) ((Byte2 << 4) | ((Byte1 >> 2) & 0x0f));\r | |
240 | *UnicodeChar = (UINT16) (UnicodeByte0 | (UnicodeByte1 << 8));\r | |
241 | \r | |
242 | default:\r | |
243 | break;\r | |
244 | }\r | |
245 | \r | |
246 | return ;\r | |
247 | }\r | |
248 | \r | |
249 | /**\r | |
250 | Translate one Unicode character into VT-UTF8 characters.\r | |
251 | \r | |
252 | UTF8 Encoding Table\r | |
253 | Bits per Character | Unicode Character Range | Unicode Binary Encoding | UTF8 Binary Encoding\r | |
254 | 0-7 | 0x0000 - 0x007F | 00000000 0xxxxxxx | 0xxxxxxx\r | |
255 | 8-11 | 0x0080 - 0x07FF | 00000xxx xxxxxxxx | 110xxxxx 10xxxxxx\r | |
256 | 12-16 | 0x0800 - 0xFFFF | xxxxxxxx xxxxxxxx | 1110xxxx 10xxxxxx 10xxxxxx\r | |
257 | \r | |
258 | \r | |
259 | @param Unicode Unicode character need translating.\r | |
260 | @param Utf8Char Return VT-UTF8 character set.\r | |
261 | @param ValidBytes The count of valid VT-UTF8 characters. If\r | |
262 | ValidBytes is zero, no valid VT-UTF8 returned.\r | |
263 | \r | |
264 | **/\r | |
265 | VOID\r | |
266 | UnicodeToUtf8 (\r | |
267 | IN CHAR16 Unicode,\r | |
268 | OUT UTF8_CHAR *Utf8Char,\r | |
269 | OUT UINT8 *ValidBytes\r | |
270 | )\r | |
271 | {\r | |
272 | UINT8 UnicodeByte0;\r | |
273 | UINT8 UnicodeByte1;\r | |
274 | //\r | |
275 | // translate unicode to utf8 code\r | |
276 | //\r | |
277 | UnicodeByte0 = (UINT8) Unicode;\r | |
278 | UnicodeByte1 = (UINT8) (Unicode >> 8);\r | |
279 | \r | |
280 | if (Unicode < 0x0080) {\r | |
281 | \r | |
282 | Utf8Char->Utf8_1 = (UINT8) (UnicodeByte0 & 0x7f);\r | |
283 | *ValidBytes = 1;\r | |
284 | \r | |
285 | } else if (Unicode < 0x0800) {\r | |
286 | //\r | |
287 | // byte sequence: high -> low\r | |
288 | // Utf8_2[0], Utf8_2[1]\r | |
289 | //\r | |
290 | Utf8Char->Utf8_2[1] = (UINT8) ((UnicodeByte0 & 0x3f) + 0x80);\r | |
291 | Utf8Char->Utf8_2[0] = (UINT8) ((((UnicodeByte1 << 2) + (UnicodeByte0 >> 6)) & 0x1f) + 0xc0);\r | |
292 | \r | |
293 | *ValidBytes = 2;\r | |
294 | \r | |
295 | } else {\r | |
296 | //\r | |
297 | // byte sequence: high -> low\r | |
298 | // Utf8_3[0], Utf8_3[1], Utf8_3[2]\r | |
299 | //\r | |
300 | Utf8Char->Utf8_3[2] = (UINT8) ((UnicodeByte0 & 0x3f) + 0x80);\r | |
301 | Utf8Char->Utf8_3[1] = (UINT8) ((((UnicodeByte1 << 2) + (UnicodeByte0 >> 6)) & 0x3f) + 0x80);\r | |
302 | Utf8Char->Utf8_3[0] = (UINT8) (((UnicodeByte1 >> 4) & 0x0f) + 0xe0);\r | |
303 | \r | |
304 | *ValidBytes = 3;\r | |
305 | }\r | |
306 | }\r | |
307 | \r | |
308 | \r | |
309 | /**\r | |
310 | Check if input string is valid VT-UTF8 string.\r | |
311 | \r | |
312 | @param TerminalDevice The terminal device.\r | |
313 | @param WString The input string.\r | |
314 | \r | |
315 | @retval EFI_SUCCESS If all input characters are valid.\r | |
316 | \r | |
317 | **/\r | |
318 | EFI_STATUS\r | |
319 | VTUTF8TestString (\r | |
320 | IN TERMINAL_DEV *TerminalDevice,\r | |
321 | IN CHAR16 *WString\r | |
322 | )\r | |
323 | {\r | |
324 | //\r | |
325 | // to utf8, all kind of characters are supported.\r | |
326 | //\r | |
327 | return EFI_SUCCESS;\r | |
328 | }\r |