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