]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Console/TerminalDxe/Vtutf8.c
ECC clean up.
[mirror_edk2.git] / MdeModulePkg / Universal / Console / TerminalDxe / Vtutf8.c
1 /** @file
2 Implementation of translation upon VT-UTF8.
3
4 Copyright (c) 2006, Intel Corporation. <BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "Terminal.h"
16
17 /**
18 Translate all VT-UTF8 characters in the Raw FIFI into unicode characters,
19 and insert them into Unicode FIFO.
20
21 @param TerminalDevice The terminal device.
22
23 @return None.
24
25 **/
26 VOID
27 VTUTF8RawDataToUnicode (
28 IN TERMINAL_DEV *TerminalDevice
29 )
30 {
31 UTF8_CHAR Utf8Char;
32 UINT8 ValidBytes;
33 UINT16 UnicodeChar;
34
35 ValidBytes = 0;
36 //
37 // pop the raw data out from the raw fifo,
38 // and translate it into unicode, then push
39 // the unicode into unicode fifo, until the raw fifo is empty.
40 //
41 while (!IsRawFiFoEmpty (TerminalDevice)) {
42
43 GetOneValidUtf8Char (TerminalDevice, &Utf8Char, &ValidBytes);
44
45 if (ValidBytes < 1 || ValidBytes > 3) {
46 continue;
47 }
48
49 Utf8ToUnicode (Utf8Char, ValidBytes, (CHAR16 *) &UnicodeChar);
50
51 UnicodeFiFoInsertOneKey (TerminalDevice, UnicodeChar);
52 }
53 }
54
55 /**
56 Get one valid VT-UTF8 characters set from Raw Data FIFO.
57
58 @param Utf8Device The terminal device.
59 @param Utf8Char Returned valid VT-UTF8 characters set.
60 @param ValidBytes The count of returned VT-VTF8 characters.
61 If ValidBytes is zero, no valid VT-UTF8 returned.
62
63 **/
64 VOID
65 GetOneValidUtf8Char (
66 IN TERMINAL_DEV *Utf8Device,
67 OUT UTF8_CHAR *Utf8Char,
68 OUT UINT8 *ValidBytes
69 )
70 {
71 UINT8 Temp;
72 UINT8 Index;
73 BOOLEAN FetchFlag;
74
75 Temp = 0;
76 Index = 0;
77 FetchFlag = TRUE;
78
79 //
80 // if no valid Utf8 char is found in the RawFiFo,
81 // then *ValidBytes will be zero.
82 //
83 *ValidBytes = 0;
84
85 while (!IsRawFiFoEmpty (Utf8Device)) {
86
87 RawFiFoRemoveOneKey (Utf8Device, &Temp);
88
89 switch (*ValidBytes) {
90
91 case 0:
92 if ((Temp & 0x80) == 0) {
93 //
94 // one-byte utf8 char
95 //
96 *ValidBytes = 1;
97
98 Utf8Char->Utf8_1 = Temp;
99
100 FetchFlag = FALSE;
101
102 } else if ((Temp & 0xe0) == 0xc0) {
103 //
104 // two-byte utf8 char
105 //
106 *ValidBytes = 2;
107
108 Utf8Char->Utf8_2[1] = Temp;
109
110 } else if ((Temp & 0xf0) == 0xe0) {
111 //
112 // three-byte utf8 char
113 //
114 *ValidBytes = 3;
115
116 Utf8Char->Utf8_3[2] = Temp;
117
118 Index++;
119
120 } else {
121 //
122 // reset *ValidBytes to zero, let valid utf8 char search restart
123 //
124 *ValidBytes = 0;
125 }
126
127 break;
128
129 case 2:
130 if ((Temp & 0xc0) == 0x80) {
131
132 Utf8Char->Utf8_2[0] = Temp;
133
134 FetchFlag = FALSE;
135
136 } else {
137
138 *ValidBytes = 0;
139 }
140 break;
141
142 case 3:
143 if ((Temp & 0xc0) == 0x80) {
144
145 Utf8Char->Utf8_3[2 - Index] = Temp;
146 Index++;
147 if (Index == 3) {
148 FetchFlag = FALSE;
149 }
150 } else {
151
152 *ValidBytes = 0;
153 Index = 0;
154 }
155 break;
156
157 default:
158 break;
159 }
160
161 if (!FetchFlag) {
162 break;
163 }
164 }
165
166 return ;
167 }
168
169 /**
170 Translate VT-UTF8 characters into one Unicode character.
171
172 UTF8 Encoding Table
173 Bits per Character | Unicode Character Range | Unicode Binary Encoding | UTF8 Binary Encoding
174 0-7 | 0x0000 - 0x007F | 00000000 0xxxxxxx | 0xxxxxxx
175 8-11 | 0x0080 - 0x07FF | 00000xxx xxxxxxxx | 110xxxxx 10xxxxxx
176 12-16 | 0x0800 - 0xFFFF | xxxxxxxx xxxxxxxx | 1110xxxx 10xxxxxx 10xxxxxx
177
178
179 @param Utf8Char VT-UTF8 character set needs translating.
180 @param ValidBytes The count of valid VT-UTF8 characters.
181 @param UnicodeChar Returned unicode character.
182
183 @return None.
184
185 **/
186 VOID
187 Utf8ToUnicode (
188 IN UTF8_CHAR Utf8Char,
189 IN UINT8 ValidBytes,
190 OUT CHAR16 *UnicodeChar
191 )
192 {
193 UINT8 UnicodeByte0;
194 UINT8 UnicodeByte1;
195 UINT8 Byte0;
196 UINT8 Byte1;
197 UINT8 Byte2;
198
199 *UnicodeChar = 0;
200
201 //
202 // translate utf8 code to unicode, in terminal standard,
203 // up to 3 bytes utf8 code is supported.
204 //
205 switch (ValidBytes) {
206 case 1:
207 //
208 // one-byte utf8 code
209 //
210 *UnicodeChar = (UINT16) Utf8Char.Utf8_1;
211 break;
212
213 case 2:
214 //
215 // two-byte utf8 code
216 //
217 Byte0 = Utf8Char.Utf8_2[0];
218 Byte1 = Utf8Char.Utf8_2[1];
219
220 UnicodeByte0 = (UINT8) ((Byte1 << 6) | (Byte0 & 0x3f));
221 UnicodeByte1 = (UINT8) ((Byte1 >> 2) & 0x07);
222 *UnicodeChar = (UINT16) (UnicodeByte0 | (UnicodeByte1 << 8));
223 break;
224
225 case 3:
226 //
227 // three-byte utf8 code
228 //
229 Byte0 = Utf8Char.Utf8_3[0];
230 Byte1 = Utf8Char.Utf8_3[1];
231 Byte2 = Utf8Char.Utf8_3[2];
232
233 UnicodeByte0 = (UINT8) ((Byte1 << 6) | (Byte0 & 0x3f));
234 UnicodeByte1 = (UINT8) ((Byte2 << 4) | ((Byte1 >> 2) & 0x0f));
235 *UnicodeChar = (UINT16) (UnicodeByte0 | (UnicodeByte1 << 8));
236
237 default:
238 break;
239 }
240
241 return ;
242 }
243
244 /**
245 Translate one Unicode character into VT-UTF8 characters.
246
247 UTF8 Encoding Table
248 Bits per Character | Unicode Character Range | Unicode Binary Encoding | UTF8 Binary Encoding
249 0-7 | 0x0000 - 0x007F | 00000000 0xxxxxxx | 0xxxxxxx
250 8-11 | 0x0080 - 0x07FF | 00000xxx xxxxxxxx | 110xxxxx 10xxxxxx
251 12-16 | 0x0800 - 0xFFFF | xxxxxxxx xxxxxxxx | 1110xxxx 10xxxxxx 10xxxxxx
252
253
254 @param Unicode Unicode character need translating.
255 @param Utf8Char Return VT-UTF8 character set.
256 @param ValidBytes The count of valid VT-UTF8 characters. If
257 ValidBytes is zero, no valid VT-UTF8 returned.
258
259 @return None.
260
261 **/
262 VOID
263 UnicodeToUtf8 (
264 IN CHAR16 Unicode,
265 OUT UTF8_CHAR *Utf8Char,
266 OUT UINT8 *ValidBytes
267 )
268 {
269 UINT8 UnicodeByte0;
270 UINT8 UnicodeByte1;
271 //
272 // translate unicode to utf8 code
273 //
274 UnicodeByte0 = (UINT8) Unicode;
275 UnicodeByte1 = (UINT8) (Unicode >> 8);
276
277 if (Unicode < 0x0080) {
278
279 Utf8Char->Utf8_1 = (UINT8) (UnicodeByte0 & 0x7f);
280 *ValidBytes = 1;
281
282 } else if (Unicode < 0x0800) {
283 //
284 // byte sequence: high -> low
285 // Utf8_2[0], Utf8_2[1]
286 //
287 Utf8Char->Utf8_2[1] = (UINT8) ((UnicodeByte0 & 0x3f) + 0x80);
288 Utf8Char->Utf8_2[0] = (UINT8) ((((UnicodeByte1 << 2) + (UnicodeByte0 >> 6)) & 0x1f) + 0xc0);
289
290 *ValidBytes = 2;
291
292 } else {
293 //
294 // byte sequence: high -> low
295 // Utf8_3[0], Utf8_3[1], Utf8_3[2]
296 //
297 Utf8Char->Utf8_3[2] = (UINT8) ((UnicodeByte0 & 0x3f) + 0x80);
298 Utf8Char->Utf8_3[1] = (UINT8) ((((UnicodeByte1 << 2) + (UnicodeByte0 >> 6)) & 0x3f) + 0x80);
299 Utf8Char->Utf8_3[0] = (UINT8) (((UnicodeByte1 >> 4) & 0x0f) + 0xe0);
300
301 *ValidBytes = 3;
302 }
303 }
304
305
306 /**
307 Check if input string is valid VT-UTF8 string.
308
309 @param TerminalDevice The terminal device.
310 @param WString The input string.
311
312 @retval EFI_SUCCESS If all input characters are valid.
313
314 **/
315 EFI_STATUS
316 VTUTF8TestString (
317 IN TERMINAL_DEV *TerminalDevice,
318 IN CHAR16 *WString
319 )
320 {
321 //
322 // to utf8, all kind of characters are supported.
323 //
324 return EFI_SUCCESS;
325 }